Reputation: 713
I'm trying to write a regex that detects multiple patterns in a text, among which a sequence of more than one space. I'm doing stuff like this:
/[abc( {2,})]/
But it seems to also detect the single space. What am I doing wrong?
Upvotes: 1
Views: 44
Reputation: 627292
The [abc( {2,})]
pattern is a character class (or bracket expression in other terminology) that matches a single char, a
, b
, c
, (
, space, {
, 2
, ,
, }
or )
. You cannot define char sequences inside character classes.
You may use
[abc]| {2,}
Or, to match any 2 or more whitespaces, use
[abc]|\s{2,}
The |
alternation operator is used to separate two alternatives here:
[abc]
- a character class (bracket expression) that matches a single char, either a
, b
or c
|
- or {2,}
- two or more spaces.NOTE: Whenever you want to use this alternation inside a longer pattern it is a good idea to wrap it with a group so that it does not "corrupt" the overall regex:
([abc]| {2,})
Or, with a non-capturing group if you needn't access to the value captured with these patterns:
(?:[abc]| {2,})
Upvotes: 3