soneedu
soneedu

Reputation: 73

RE match fail in python, confuse with the result on regex101

http://regex101.com/r/oU6eI5/1 , test here seam works, but when i put in Python, match whole str.

str = galley/files/tew/tewt/tweqt/
re.sub('^.+/+([^/]+/$)', "\1", str) 

i want get "tweqt/"

Upvotes: 1

Views: 603

Answers (2)

Jerry
Jerry

Reputation: 71538

You need to use a raw string in the replace:

str = galley/files/tew/tewt/tweqt/
re.sub('^.+/+([^/]+/$)', r"\1", str)
#                        ^

Otherwise, you get the escaped character \1. For instance on my console, it's a little smiley.

If you somehow don't want to raw your string, you'll have to escape the backslash:

re.sub('^.+/+([^/]+/$)', "\\1", str)

Also worth noting that it's a good practice to raw your regex strings and use consistent quotes, so you I would advise using:

re.sub(r'^.+/+([^/]+/$)', r'\1', str)

Other notes

It might be simpler to match (using re.search) instead of using re.sub:

re.search(r'[^/]+/$', str).group()
# => tweqt/

And you might want to use another variable name other than str because this will override the existing function str().

Upvotes: 5

Avinash Raj
Avinash Raj

Reputation: 174706

It would be better if you define the pattern or regex as raw string.

>>> import re
>>> s = "galley/files/tew/tewt/tweqt/"
>>> m = re.sub(r'^.+/+([^/]+/$)', r'\1', s)
               ^                  ^
>>> m
'tweqt/'

Upvotes: 3

Related Questions