1

I have a regular expression

regex = '[\w!\-$%^&*()_+|~=`{}\[\]:";\'<>?,.@#\\/]+' 

When i try to match the regular expression with a string for example

password = '\Gs7iCHE' 

no match is found but when i change the regular expression to

regex = '[\w!\-$%^&*()_+|~=`{}\[\]:";\'<>?,.@#\\\\/]+' 

it finds a match which is

match = '\\Gs7iCHE' 

I dont know why the behavior is this way.

9
  • 1
    What regex flavor are you using? Commented Apr 22, 2016 at 14:36
  • "In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (), the caret (^), and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash." Commented Apr 22, 2016 at 14:40
  • If you're using PCRE, it looks like it doesn't like the last "/": regex101.com/r/rI0vW6/1 Commented Apr 22, 2016 at 14:44
  • Is this javascript? In that case the backslash \ needs to be escaped to have it in a string. And since the escaped \ needs to be escaped in the character set, you have to enter \\\\. In your first case it escapes the /. Commented Apr 22, 2016 at 14:48
  • Also, the other escapes just makes the javastring escape, thus you escape w and adds a range !-$. Commented Apr 22, 2016 at 14:54

4 Answers 4

2

Your regex is correct. Use raw string r before regex and it will work fine

re.compile(r'[\w!\-$%^&*()_+|~=`{}\[\]:";\'<>?,.@#\\/]+') 

Check

Sign up to request clarification or add additional context in comments.

Comments

0

The backslash \ needs to be escaped to have it in a string. And since the escaped \ needs to be escaped in the character set, you have to enter \\\\. In your first case it escapes the /

Also, the other escapes just makes the string escape, thus you escape wand adds a range !-$.

In other words - try:

regex = '[\\w!\\-$%^&*()_+|~=`{}[\\]:";\'<>?,.@#\\\\/]+' 

No need to escape the [.

Regards

1 Comment

The regex is correct but it still returns double `\` in the password string which was my question.
0

When you changed your regex to the second one \\\\
parses into \\ for single quote strings.

This is ok and all, but now your regex matches the literal escape.
And since its inside a quantified character class [\\]+, it's going
to match as many escapes as in your target string.

Your target string is \\Gs7iCHE and sure enough, it matches the entire thing.

One thing to note about escaping single quote strings,
This '\\\' is identical to '\\\\' after parsing. Namely, they both
become \\.

Comments

0

In javascript, I came across this kind of an issue once. In JavaScript (and many other languages) RegExp is delimited by slashes, followed by mode flags. If you want to use escaped characters, you have to include your regexp inside //.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.