Hi I am trying to understand python code which has this regular expression re.compile(r'[ :]')
. I tried quite a few strings and couldnt find one. Can someone please give example where a text matches this pattern.
Asked
Active
Viewed 145 times
-2
![](../../users/profiles/3727165.webp)
Terminator
- 97
- 1
- 10
-
3`[]` means "any character from between these brackets". So your expression will match any space or colon character. – khelwood Nov 28 '14 at 17:40
-
The reference post links to [How to back reference "inner" selections ( () ) in a regular expression?](http://stackoverflow.com/a/1553171) for character classes. – Martijn Pieters Nov 28 '14 at 17:42
-
There are only two strings the pattern matches. `' '` and `':'`. Depending on how you *use* the pattern, those strings can be part of a larger string. – Martijn Pieters Nov 28 '14 at 17:43
2 Answers
0
The expression simply matches a single space or a single :
(or rather, a string containing either). That’s it. […]
is a character class.
![](../../users/profiles/1968.webp)
Konrad Rudolph
- 482,603
- 120
- 884
- 1,141
0
The []
matches any of the characters in the brackets. So [ :]
will match one character that is either a space or a colon.
So these strings would have a match:
"Hello World"
"Field 1:"
etc...
These would not
"This_string_has_no_spaces_or_colons"
"100100101"
Edit: For more info on regular expressions: https://docs.python.org/2/library/re.html
![](../../users/profiles/733547.webp)
Bryce Siedschlaw
- 3,877
- 1
- 21
- 33
-
I tried as the pattern r'[ :]'and Hello World as the test string in http://pythex.org/ but it doesn't match. – Terminator Nov 28 '14 at 17:49
-