0

this is a quick question:

How would I specify a regex which can be used several times with multiple match strings? I might not have worded that right, but I will try to show some code.

I have this regex:

regex = compile(r'(?=(%s))')

In a for loop, I will try and match the string I have to one I specify for the regex so that at each iteration, I can change the string being matched and it will try to match it.

So is this possible, can I do something like

regex.findall(myStringString, myMatchString)

in the code or would I have to recompile the regex in order for it to match a new string?

More clarification: I want to do this:

re.findall('(?=(%s))' %myMatchString, mySearchString)

but because myMatchString will be changing at each iteration of the loop, I want to do it like this so I can match the new string:

regex = re.compile(r'(?=(%s))')
regex.findall( myMatchString, mySearchString)

Thanks for reading

dotancohen
  • 30,064
  • 36
  • 138
  • 197
smac89
  • 39,374
  • 15
  • 132
  • 179

2 Answers2

1

The point of re.compile is to explicitly declare you're going to re-use the same pattern again and again - and hopefully avoid any compilation that may be required.

As what you're doing is not necessarily re-using the same pattern, then you're better off letting the re system cache patterns (it caches n many - but can't remember exactly how many), and just use re.findall(...)/whatever your regex afresh each time.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • By default, `re` captures 100 items in its `_MAXCACHE` attribute. This can be changed. http://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile – Kevin London Nov 14 '16 at 23:15
1

well, if I understand what you say, all you want to write is :

def match_on_list_of_strings(list_of_strings):
    regex = compile(r'(?=(%s))')
    for string in list_of_strings:
        yield regex.findall(string)

That will apply your match on the strings as many times there are strings in the list of strings, while your regex been compiled only once.

Aaaah... but you don't need a regex for that:

def match_on_list_of_strings(bigstring, list_of_strings):
    for string in list_of_strings:
        if string in bigstring:
            yield string

or if you really want to use a re:

def match_on_list_of_strings(bigstring, list_of_strings):
    for string in list_of_strings:
        if re.match('.*'+string+'.*', bigstring):
            yield string

And then to answer your question, no you can't compile the destination string into a regex, but only the contrary. When you compile a regex, what you do is transform the actual regexp into an internal representation of the automaton. You might want to read courses on NFA and regexps

zmo
  • 24,463
  • 4
  • 54
  • 90
  • Not quite but very close to what I want to do. I have a Static string if you may. i.e. it never changes. Then from your list_of_strings I want to see how many times each string can be found in my 'Static string' – smac89 Jun 25 '13 at 22:06