Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question mark with equal to sign in regex

Tags:

regex

Hey Experts i am new to regex.I am really confused by studying this regex.I have found something which is very difficult to understand for me.The thing is the use of question mark and equal to symbol in regex.An eg :

"(?<=\d)(\s)(?=[\d-])"

I just need to know the use of ?= in this regex code..I have searched google many times in this case but i didnt find any solution there.So i came here It will be a great help for me if you answer this one correctly for me ..:) ..

Thanks in advance ..

like image 789
badu Avatar asked Jan 08 '14 14:01

badu


People also ask

What does a question mark mean in regex?

A question mark ( ? ) immediately following a character means match zero or one instance of this character . This means that the regex Great!? will match Great or Great! .

How do you match a question mark in regex?

But if you want to search a question mark, you need to “escape” the regex interpretation of the question mark. You accomplish this by putting a backslash just before the quesetion mark, like this: \? If you want to match the period character, escape it by adding a backslash before it.

What does ?= * Mean in regex?

. means match any character in regular expressions. * means zero or more occurrences of the SINGLE regex preceding it.

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.


2 Answers

This is a lookahead.

The part before is only matched if followed by [\d-]

You should notice the start of the expression is, symmetrically, a lookbehind.

Both groups are not capturing. To sum it up, this regular expression matches a space following a digit and followed either by a digit or a minus sign. For example it matches the space in "3 4".

Be careful that many languages/engines don't support lookbehind, for performance and predictability reason (see this interesting article for example).

like image 186
Denys Séguret Avatar answered Sep 20 '22 03:09

Denys Séguret


At least in JavaScript, the ?= matches a suffix but excludes it from capture. ?= excludes the expression from the entire match. For more information, see this question and it's corresponding answers.

like image 36
War10ck Avatar answered Sep 19 '22 03:09

War10ck