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 156 times
-2

Terminator
- 117
- 1
- 12
-
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.

Konrad Rudolph
- 530,221
- 131
- 937
- 1,214
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

Bryce Siedschlaw
- 4,136
- 1
- 24
- 36
-
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
-