Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to generate strings for case insensitive matches #6

Open
rutsky opened this issue Jun 28, 2013 · 2 comments
Open

Option to generate strings for case insensitive matches #6

rutsky opened this issue Jun 28, 2013 · 2 comments
Assignees

Comments

@rutsky
Copy link

rutsky commented Jun 28, 2013

Maybe you will find it a nice and simple to implement feature: to be able to iterate over all strings that matches regular expression ignoring letters case.
E.g. "a{1,2}" will generate "aa", "aA", "Aa", "AA".

P.S. Thanks for this library, it helped me a lot!

@ghost ghost assigned asciimoo Jun 28, 2013
@asciimoo
Copy link
Owner

Yes, it would be a great feature.
But it isn't really easy to implement. Need to permute the case of all letters of constant strings in the regex, because all of "abc", "aBc", "abC", "ABc", "AbC", "aBC", "ABC" matched by "abc" case-insensitively
[aA][bB][cC] produces the same result set and works with the current version

@rutsky
Copy link
Author

rutsky commented Jun 28, 2013

How about following algorithm?

  1. translate regular expression to lower case:
[Aab]{1,2} -> [aab]{1,2}
  1. remove redundancies (not sure how they treated in exrex):

    [aab]{1,2} -> [ab]{1,2}

  2. generate strings using exrex

  3. for each generated string s generate all strings s_i with permuted cases, such as that s_i.lower() == s:

def gen_case_strings(s):
   if s:
       head, tail = s[:-1], s[-1]
       for head_cased in gen_case_strings(head):
           yield head_cased + tail.lower()
           yield head_cased + tail.upper()
   else:
       yield s

So that:

>>> list(gen_case_strings("abc"))
['abc', 'abC', 'aBc', 'aBC', 'Abc', 'AbC', 'ABc', 'ABC']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants