0

Today, I found out that regex r"['a', 'b']" matches 'a, b'.

Why is that? What does comma and ' mean inside []?

Thank you.

Paul R
  • 2,631
  • 3
  • 38
  • 72

1 Answers1

2

[] is used to define character sets in regular expressions. The expression will match if the string contains any of the characters in that set.

Your regular expression:

 r"['a', 'b']"

Says "match if string contains ' or a or , or b. As @Patrick Haugh mentions in his comment. Your expression is equivalent to [',ab]. Repeating the same character in the set does nothing.

http://www.regexpal.com/ is a great site for testing your regular expressions. It can help break it down for you and explain what your expression does and why it matches on certain strings.

TheF1rstPancake
  • 2,318
  • 17
  • 17