valueerror('cannot process flags argument with a compiled pattern')

  • Last Update :
  • Techknowledgy :

You need to place the modifier inside your re.compile pattern, also good to use raw string notation here.

regex = re.compile(r "DataStore\.prime\('standings', { stageId: \d+ }.*", re.S) ^
   ^ ^ ^

Suggestion : 2

I am using Python.org version 2.7 64 bit on Windows Vista 64 bit. I am using a regex with Scrapy to parse the data contained within the first Javascript item called 'Datastore.prime' within the following page:,Python – How to pass a list as a command-line argument with argparse,Python – how can i obtain pattern string from compiled regexp pattern in python,However this is throwing up the error within the title of this post:

Link here
The regex I am using is this:

regex = re.compile('DataStore\.prime\(\'standings\', { stageId: \d+ }.*')
match2 = re.findall(regex, response.body, re.S)
match3 = str(match2)
match3 = match3.replace('<a class="w h"', '').replace('<a class="w a"', '').replace('<a class="d h"', '')\
   .replace('<a class="d a"', '').replace('<a class="l h"', '').replace('<a class="l a"', '')\
   .replace('title=', '')
print match3

However this is throwing up the error within the title of this post:

     raise ValueError('Cannot process flags argument with a compiled pattern')
     exceptions.ValueError: Cannot process flags argument with a compiled pattern

You need to place the modifier inside your re.compile pattern, also good to use raw string notation here.

regex = re.compile(r "DataStore\.prime\('standings', { stageId: \d+ }.*", re.S) ^
   ^ ^ ^

Suggestion : 3

Just installed the extension and when trying it out (with a ticket that contains the example on the MarkdownMacro page I get the following error: , I had the same problem using Python 2.6 and managed to fix the macro by a small modification to markdown/0.11/Markdown/macro.py. Change row 27-30 (for markdown macro 0.11.1) , Apparently it is no longer possible to recompile regexps with flags in Python > 2.5 , This ticked is used by spammers, I'm deleting my account now moved on to ChiliProject anyway since trac seems to be dead.

Just installed the extension and when trying it out (with a ticket that contains the example on the MarkdownMacro page I get the following error:

File "/var/tmp/trac-test/egg-cache/Trac-0.12.2-py2.7.egg-tmp/trac/ticket/templates/ticket_box.html", line 77, in <Expression u'wiki_to_html(context, ticket.description, escape_newlines=preserve_newlines)'>
  ${wiki_to_html(context, ticket.description, escape_newlines=preserve_newlines)}
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 1497, in format_to_html
  return HtmlFormatter(env, context, wikidom).generate(escape_newlines)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 1452, in generate
  escape_newlines)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 1201, in format
  self.handle_code_block(line, block_start_match)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 1091, in handle_code_block
  processed = self.code_processor.process(code_text)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 304, in process
  text = self.processor(text)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/trac/wiki/formatter.py", line 291, in _macro_processor
  text)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/Markdown/macro.py", line 60, in expand_macro
  return markdown(sub(LINK, convert, content))
File "/usr/local/lib/python2.7/re.py", line 151, in sub
  return _compile(pattern, flags).sub(repl, string, count)
File "build/bdist.freebsd-8.2-RELEASE-amd64/egg/Markdown/macro.py", line 51, in convert
  I).groups()[0]
File "/usr/local/lib/python2.7/re.py", line 142, in search
  return _compile(pattern, flags).search(string)
File "/usr/local/lib/python2.7/re.py", line 237, in _compile
  raise ValueError('Cannot process flags argument with a compiled pat

System Information:

User Agent: Mozilla/5.0 (Macintosh; PPC Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko) Version/5.0.6 Safari/533.22.3

Trac	0.12.2
Babel	0.9.6
Docutils	0.8
Genshi	0.6
Mercurial	1.9.1
Pygments	1.4
pysqlite	2.6.0
Python	2.7.2 (default, Sep 2 2011, 10:31:59) [GCC 4.2.1 20070719 [FreeBSD]]
pytz	2011g
setuptools	0.6c11
SilverCity	0.9.7
SQLite	3.7.7.1
jQuery	1.4.2
Enabled Plugins:

TracMarkdownMacro	0.11.1	/var/tmp/trac-test/plugins/TracMarkdownMacro-0.11.1-py2.7.egg
TracMercurial	0.12.0.28dev-r10784	/var/tmp/trac-test/plugins/TracMercurial-0.12.0.28dev_r10784-py2.7.egg

I had the same problem using Python 2.6 and managed to fix the macro by a small modification to markdown/0.11/Markdown/macro.py. Change row 27-30 (for markdown macro 0.11.1)

LINK = compile(
   r '(\]\()([^) ]+)([^)]*\))|(<)([^>]+)(>)|(\n\[[^]]+\]: *)([^ \n]+)(.*\n)'
)
HREF = compile(r 'href=[\'"]?([^\'" ]*)')

into

LINK = r '(\]\()([^) ]+)([^)]*\))|(<)([^>]+)(>)|(\n\[[^]]+\]: *)([^ \n]+)(.*\n)'
HREF = r 'href=[\'"]?([^\'" ]*)'

Suggestion : 4

You’ve learned about the re.compile(pattern) method that prepares the regular expression pattern—and returns a regex object which you can use multiple times in your code.,The re.search(pattern, string) method is a mere wrapper for compiling the pattern first and calling the p.search(string) function on the compiled regex object p.,This two-step approach is more efficient than calling, say, search(pattern, string) at once. That is, IF you call the search() method multiple times on the same pattern. Why? Because you can reuse the compiled pattern multiple times.,The re.compile(patterns, flags) method returns a regular expression object. You may ask (and rightly so):

Here’s an example:

import re

# These two lines...
   regex = re.compile('Py...n')
match = regex.search('Python is great')

#...are equivalent to...
   match = re.search('Py...n', 'Python is great')

In both instances, the match variable contains the following match object:

<re.Match object; span=(0, 6), match='Python'>

Specification:

re.compile(pattern, flags = 0)

Consider the following example:

import re

# These two lines...
   regex = re.compile('Py...n')
match = regex.search('Python is great')

#...are equivalent to...
   match = re.search('Py...n', 'Python is great')

Here’s the relevant code snippet from re’s GitHub repository:

#–-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# internals

_cache = {}
# ordered!

   _MAXCACHE = 512
def _compile(pattern, flags):
   # internal: compile pattern
if isinstance(flags, RegexFlag):
   flags = flags.value
try:
return _cache[type(pattern), pattern, flags]
except KeyError:
   pass
if isinstance(pattern, Pattern):
   if flags:
   raise ValueError(
      "cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
   raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
if not(flags & DEBUG):
   if len(_cache) >= _MAXCACHE:
   # Drop the oldest item
try:
del _cache[next(iter(_cache))]
except(StopIteration, RuntimeError, KeyError):
   pass
_cache[type(pattern), pattern, flags] = p
return p

Suggestion : 5

I can't remember... IIRC, I installed from source (0.7.9) using python setup.py install... Right now, I can't reproduce either -> closing.,By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.,Unfortunately, I am unable to reproduce the issue you encountered., The text was updated successfully, but these errors were encountered:

machine:/tmp/logfiles# logrep -f 'status>399' -o 'count(*),status,url' -s '10:1' logfile.log
Traceback (most recent call last):
File "/usr/local/bin/logrep", line 5, in <module>
   pkg_resources.run_script('wtop==0.7.9', 'logrep')
   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 499, in run_script
   self.require(requires)[0].run_script(script_name, ns)
   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1235, in run_script
   execfile(script_filename, namespace, namespace)
   File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/EGG-INFO/scripts/logrep", line 427, in <module>
      runit()
      File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/EGG-INFO/scripts/logrep", line 401, in runit
      order_descending, x_tmp_file)
      File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 1026, in calculate_aggregates
      for r in reqs:
      File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 810, in fn
      first = lst.next()
      File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 479, in iis_field_map
      for record in log:
      File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 491, in <genexpr>
         log = (dict(zip(cols, urllib.unquote_plus(t))) for t in tuples)
         File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 490, in <genexpr>
            tuples = (line.split(" ") for line in loglines)
            File "/usr/local/lib/python2.7/dist-packages/wtop-0.7.9-py2.7.egg/logrep.py", line 717, in line_exclude
            r = re.compile(pat, re.I)
            File "/usr/lib/python2.7/re.py", line 190, in compile
            return _compile(pattern, flags)
            File "/usr/lib/python2.7/re.py", line 235, in _compile
            raise ValueError('Cannot process flags argument with a compiled pattern')
            ValueError: Cannot process flags argument with a compiled pattern

Suggestion : 6

I am using the search function in a regex anycodings_regex expression in a while loop. But the program anycodings_regex ends with ValueError: cannot process flags anycodings_regex argument with a compiled pattern. If I use a anycodings_regex pattern that is not compiled, it ends with: anycodings_regex ValueError: ASCII and UNICODE flags are anycodings_regex incompatible. I use Python 3.81. How to fix anycodings_regex this? ,(I was able to run the program successfully anycodings_regex with finditer. ,The third argument of re.search defines anycodings_python the flags.,Anytime I run my code (any code by the way) whether it's the default code or my personal project, I keep getting the same error, no matter how simple

(I was able to run the program successfully anycodings_regex with finditer.

#!/usr/bin/python3

import re

text = 'This island is beautiful and is large'

pattern = re.compile(r '\bis\b')
# pattern = r '\bis\b'

idx = 0
# match = re.search(pattern, text, pos = idx)

while True:
   #
while (match: = re.search(pattern, text, idx)):
   # pattern = re.compile(r '\bis\b')
match = re.search(pattern, text, idx)
if match == None:
   break
print(match.group())
idx += match.endpos

If you want to specify the position, anycodings_python use:

 Pattern.search(string[, pos[, endpos]])