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

re.error: nothing to repeat at position 0 #461

Open
mennatallah644 opened this issue Apr 5, 2022 · 3 comments
Open

re.error: nothing to repeat at position 0 #461

mennatallah644 opened this issue Apr 5, 2022 · 3 comments

Comments

@mennatallah644
Copy link

I am facing this issue when i am trying to loop on certain expressions to find their occurences in string

I am using the last version of the package

here is the code

matches = re.finditer(r"\b{}\b".format(substring), string)
OFFSETS = [(_.start(), _.end()) for _ in matches ]

the error occurs in the line where i use finditer
it says
re.error: nothing to repeat at position 0

@mrabarnett
Copy link
Owner

The .format is just building a string that will be used as a pattern. If the resulting string is not a valid pattern, then the module will complain when it tries to compile it.

Whatever you have in substring is not a valid pattern.

@mennatallah644
Copy link
Author

mennatallah644 commented Apr 5, 2022

@mrabarnett
actually I was passing the substring pattern which is starting with * but i didn't mean to use it as regex character and after i used the re.escape()

matches = re.finditer(r"\b{}\b".format(re.escape(substring)), string)
OFFSETS = [(_.start(), _.end()) for _ in matches ]

the error gone but no matching came even the substring already exists in the string
ex:
substring = '*Hello'
String = "*Hello world"

why no matching came even the substring exists in the string ?

@mrabarnett
Copy link
Owner

The pattern you're building is \b\*Hello, which will match a word boundary, then a *, then Hello. The string you have starts with a *, followed by Hello. There's no word boundary before the *.

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

No branches or pull requests

2 participants