28

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 ..

badu
  • 999
  • 3
  • 9
  • 11
  • What system or language is this regex used in? – lurker Jan 08 '14 at 14:23
  • 5
    This is a [lookahead assertion](http://www.regular-expressions.info/lookaround.html). I find [this cheat sheet](http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) especially helpful for understanding all of the special regex characters – sjagr Jan 08 '14 at 14:26
  • "Lookahead assertions" are called "positive lookaheads", too - just if you stumble upon this – KeyNone Jan 08 '14 at 14:29
  • 8
    +1 for mentioning "question mark" and "equals" in your text. This is what I call search engine friendly :) – Sebastian Jan 11 '16 at 09:51

2 Answers2

23

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).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
14

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.

Community
  • 1
  • 1
War10ck
  • 12,387
  • 7
  • 41
  • 54
  • This can be used nicely for password validation: `^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*.!@$%^&(){}[]:;<>,.?/~_+-=|\]).{8,32}$` https://www.ocpsoft.org/tutorials/regular-expressions/password-regular-expression/ – halllo Jun 25 '20 at 20:07
  • Great answers! @halllo, is there any reason you included the period in the list twice (it is the 2nd and 20th in the list)? – Tom Hubbard Sep 02 '23 at 02:04
  • Also @halllo, in python3 the square brackets don't work in this regex: ``` >>> import re >>> print(re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@$%^&(){}[]]).{8,}$', 'Testing13!')) None >>> print(re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@$%^&(){}]).{8,}$', 'Testing13!')) ``` – Tom Hubbard Sep 02 '23 at 03:00