0

I'm trying to get the regex of a backslash, if the backslash is not present in the test string and not in the regex expression it is working, I tried \\ and \\\\

This is my code:

def getString():
    test=',"string":"x\u002fg06HaX0M\u002fuTJh7Q",Bla'
    m=re.match(r",\"string\":\"([\w\d\\\\]+)\",",test)
    print(m.group(1))

The error i get is:

AttributeError: 'NoneType' object has no attribute 'group'

Thanks for your help.

0

1 Answer 1

2

It seems like unicode chars present in your input string (\u002f). There isn't an actual backslash character present in the input. And also I think you want to get the characters present inside the double quotes which exists next to "string":. So use a negated character class like [^"]+ to match any character (including unicodes) but not of double quotes, zero or more times.

>>> st=',"string":"x\u002fg06HaX0M\u002fuTJh7Q",Bla'
>>> re.match(u',"string":"([^"]+)",',st).group(1)
'x/g06HaX0M/uTJh7Q'
>>> re.match(r',"string":"([^"]+)",',st).group(1)
'x/g06HaX0M/uTJh7Q'
Sign up to request clarification or add additional context in comments.

3 Comments

Why there isn't actual backslash?
\u002f is a Unicode representation of a single character.
p='' for line in text.splitlines(): if 'string' in line: for part in line.split(','): if 'string' in part: p=part m=re.match(u'.*"string":"([^"]+)"',p).group(1) Any idea why this code doesn't decode the unicode characters? print(m) gives string with \u002f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.