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

simplify screws up literal special characters #28

Closed
iamthad opened this issue Nov 6, 2017 · 4 comments
Closed

simplify screws up literal special characters #28

iamthad opened this issue Nov 6, 2017 · 4 comments
Labels

Comments

@iamthad
Copy link

iamthad commented Nov 6, 2017

simplify turns a literal period into an unescaped period.

>>> import exrex
>>> exrex.simplify(r'\.')
'.'
>>> exrex.simplify('[.]')
'.'

EDIT: This affects any single special character.

@asciimoo asciimoo added the bug label Nov 6, 2017
@iamthad iamthad changed the title simplify screws up literal period simplify screws up literal special characters Nov 6, 2017
@erzvtvhfm-fhjnyfxv
Copy link

erzvtvhfm-fhjnyfxv commented Dec 19, 2018

Issue still exists 😞 Used version is exrex-0.10.5.

$ exrex -s '\..'
..

@vwyu
Copy link
Contributor

vwyu commented Jan 9, 2019

It seems that the issue is due to the following lines of sre_to_string method in exrex.py:

elif i[0] == sre_parse.LITERAL:
    ret += unichr(i[1])

The issue appears to be resolved, if we change these lines as follows:

elif i[0] == sre_parse.LITERAL:
    u = unichr(i[1])
    ret += u if u not in sre_parse.SPECIAL_CHARS else '\\{0}'.format(u)

After the change, I get:

>>> import exrex
>>> exrex.simplify(r'\.')
'\\.'
>>> exrex.simplify('[.]')
'\\.'
>>> exrex.simplify('.')
'.'
>>> exrex.simplify(r'\$')
'\\$'
>>> exrex.simplify('[$]')
'\\$'
>>> exrex.simplify('[a]')
'a'

I belive that exrex.simplify('[.]') returning '\\.' and exrex.simplify('[$]') returning '\\$' are correct behaviors, since both cases involve special characters in ranges [].

@iamthad
Copy link
Author

iamthad commented Jan 10, 2019

@vwyu That does indeed appear to fix the issue. Do you want to make a pull request?

@scopatz
Copy link

scopatz commented Mar 21, 2019

I am also seeing this on 0.10.5 with [*]

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

No branches or pull requests

5 participants