-3

I understand the meaning of [A-Za-z0-9_]+ corresponding to a repeated sequence of one or more characters containing upper case letters, lower case letters, digits and underscores, but what does the whole expression corresponds to?

recnac
  • 3,511
  • 6
  • 21
  • 43
Haos
  • 1
  • 1
  • That depends on to what text you are applying the regex. – Tim Biegeleisen Jul 31 '18 at 01:58
  • `(?=\\s+)` means lookahead for a literal backslash, followed by repeated `s`s. (or if that's meant to be a single backslash, then lookahead for repeated space characters. In other words, word characters followed by spaces?) – CertainPerformance Jul 31 '18 at 01:59
  • Unless that regex was declared in a string @CertainPerformance. Then it could possibly be inferred as word characters followed by multiple spaces. – Sunny Patel Jul 31 '18 at 02:04
  • Possible duplicate of [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Ken White Jul 31 '18 at 02:37
  • Possible duplicate of [What does ?= mean in a regular expression?](https://stackoverflow.com/questions/1570896/what-does-mean-in-a-regular-expression) – shreyasminocha Jul 31 '18 at 06:20

1 Answers1

0

I'm going to assume that your regex is /[A-Za-z0-9_]+(?=\s+)/ and that your programming language requires you to escape the \ as \\.

Like you said, [A-Za-z0-9_]+ matches one or more alpha-numeric characters.

The (?=) pattern indicates a positive look ahead expression. We are checking if after the alpha-numeric characters, we have one or more(+) whitespace(\s) characters. However, the difference between /[A-Za-z0-9_]+\s+/ and /[A-Za-z0-9_]+(?=\s+)/ is that the former would include the whitespace in the match while the latter will not.

If you run your regex on this_is_followed_by_whitespace␠␠␠ where "␠" indicates spaces, only this_is_followed_by_whitespace will be matched. The expression is just looking ahead to check whether there is whitespace. Running /[A-Za-z0-9_]+\s+/ on the same string would match this_is_followed_by_whitespace␠␠␠.

Play around with your regex on this RegExr demo.

shreyasminocha
  • 520
  • 3
  • 13