Wednesday, 26 July 2017

python - Python3 - Generate string matching multiple regexes, without modifying them



I would like to generate string matching my regexes using Python 3. For this I am using handy library called rstr.






  • ^[abc]+.

  • [a-z]+





I must find a generic way, how to create string that would match both my regexes.





Modify both regexes or join them in any way. This I consider as ineffective solution, especially in the case if incompatible regexes:



import re
import rstr

regex1 = re.compile(r'^[abc]+.')
regex2 = re.compile(r'[a-z]+')

for index in range(0, 1000):
generated_string = rstr.xeger(regex1)
if re.fullmatch(regex2, generated_string):
break;
else:
raise Exception('Regexes are probably incompatibile.')

print('String matching both regexes is: {}'.format(generated_string))


Is there any workaround or any magical library that can handle this? Any insights appreciated.







Asker already has the string, which he just want to check against multiple regexes in the most elegant way. In my case we need to generate string in a smart way that would match regexes.


Answer



I solved this using a little alternative approach. Notice second regex is basically insurance so only lowercase letters are generated in our new string.



I used Google's python package sre_yield which allows charset limitation. Package is also available on PyPi. My code:



import sre_yield
import string

sre_yield.AllStrings(r'^[abc]+.', charset=string.ascii_lowercase)[0]
# returns `aa`

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...