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 ..
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! .
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.
. means match any character in regular expressions. * means zero or more occurrences of the SINGLE regex preceding it.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With