Reputation: 3846
What I really need to know is :
(?(
mean ??:
mean ?The regex I am trying to figure out is :
(notice the above mentioned symbols in the following regex)
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
Upvotes: 7
Views: 2155
Reputation: 32797
(?(?=and )(and )|(blah))
pattern is used like if-then-else like (?(expression)yes|no)
i.e and
would be matched if and
is there else blah
would be matched
(?:)
is a non capturing group.So it would not be included in the group or be used as back-reference \1
So,
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
would match
and 1900
blah2000
and 2012
blah2013
NOTE(it's all about the groups)
The samething can be achievend with this regex
(and |blah)(?:[1][9]|[2][0])[0-9][0-9]
.
The only thing in which these regex differ is the number of groups formed.
So my regex would form 1 group which would contain either and
or blah
Your regex would form no groups.It will form a group only if it matches blah
..
Upvotes: 3
Reputation: 1550
?:
is a non-capturing group.
(?ifthen|else)
is used to construct if, then expression.
You can read more about these here.
http://www.regular-expressions.info/conditional.html
Upvotes: 0
Reputation: 336128
(?:...)
is a non-capturing group. It works just like (...)
, but it doesn't create a backreference (\1
etc.) for later re-use.
(?(condition)true|else)
is a conditional which tries to match condition
; if that succeeds, it will then try to match true
, if not, it will try to match else
.
This is a rarely seen regex construct because there are not too many use cases for it. In your case,
(?(?=and )(and )|(blah))
could have been rewritten as
(and |blah)
Upvotes: 2
Reputation: 10146
Here's a quick reference to some patterns:
. Any character except newline.
\. A period (and so on for \*, \(, \\, etc.)
^ The start of the string.
$ The end of the string.
\d,\w,\s A digit, word character [A-Za-z0-9_], or whitespace.
\D,\W,\S Anything except a digit, word character, or whitespace.
[abc] Character a, b, or c.
[a-z] a through z.
[^abc] Any character except a, b, or c.
aa|bb Either aa or bb.
? Zero or one of the preceding element.
* Zero or more of the preceding element.
+ One or more of the preceding element.
{n} Exactly n of the preceding element.
{n,} n or more of the preceding element.
{m,n} Between m and n of the preceding element.
??,*?,+?,
{n}?, etc. Same as above, but as few as possible.
(expr) Capture expr for use with \1, etc.
(?:expr) Non-capturing group.
(?=expr) Followed by expr.
(?!expr) Not followed by expr.
The expression (?(?=and )(and )|(blah))
is an if-else
expression:)
You can test reqular expressions here: Regexpal.com
Upvotes: 2