Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this regex mean

Tags:

c#

regex

What I really need to know is :

  1. What does (?( mean ?
  2. What does ?: 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]
like image 979
harshit Avatar asked Oct 17 '12 11:10

harshit


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.

What does this regex expression do?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions.


1 Answers

(?(?=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..

like image 51
Anirudha Avatar answered Oct 15 '22 01:10

Anirudha