0

Can someone please explain this regex?

^(?=.*prodCode=).*$
Joey
  • 316,376
  • 76
  • 642
  • 652
rtv
  • 157
  • 1
  • 9
  • 3
    There are different regExp in title and body of question. Correct it please! – Sergii Stotskyi Jul 20 '12 at 07:46
  • 3
    Note that it is exactly the same as `^.*prodCode=.*$`, the look-ahead here is pointless. – Keppil Jul 20 '12 at 07:57
  • I don't think this regex would match anything. The first ".*" would greedily match everything on the line and there would never be anything left to match "prodCode=". – Ted Hopp Jul 20 '12 at 07:57
  • Ted, the regex in the question has the first `.*` in a lookahead so the `.*` there doesn't take anything away that could make `prodCode=` match. Keppil's regex should also work because the engine will backtrack and try to make `prodCode=` match. – Joey Jul 20 '12 at 08:06
  • 1
    Does this answer your question? [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – GalaxyCat105 Dec 15 '20 at 13:11

3 Answers3

9

From this nice regex explainer:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                      the beginning of the string
--------------------------------------------------------------------------------
  (?=                    look ahead to see if there is:
--------------------------------------------------------------------------------
  .                      any character except \n
--------------------------------------------------------------------------------
  prodCode=              'prodCode='
--------------------------------------------------------------------------------
  )                      end of look-ahead
--------------------------------------------------------------------------------
  .                      any character except \n
--------------------------------------------------------------------------------
  $                      before an optional \n, and the end of the string

EDIT

Since the regex in the text of the question has changed, the next-to-last line in the explanation would change to:

--------------------------------------------------------------------------------
  .*                     any character except \n (0 or more times (matching
                         the most amount possible))
--------------------------------------------------------------------------------
Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
  • 2
    There is `^(?=.*prodCode=).*$` in the title of question – Sergii Stotskyi Jul 20 '12 at 07:44
  • @Serjio - How about that. It would change the next-to-last line explanation to: _"any character except \n (0 or more times (matching the most amount possible))"_ – Ted Hopp Jul 20 '12 at 07:46
  • Thanks!! so this would match a line starts with prodCode=XXXX and does not match #prodCode=xxxx or someProdCode=xxxx right. – rtv Jul 20 '12 at 07:47
  • @rtv - There's a '.' before "prodCode=" in the regex, so there has to be (exactly) one character before "prodCode=". It would not match a line starting with "prodCode=" but it would match a line starting with "#prodCode=". – Ted Hopp Jul 20 '12 at 07:49
  • @TedHopp - I was thinking that .* is Zero or more chars, is it wrong? – rtv Jul 20 '12 at 07:53
  • @rtv - Oops. I missed the "*" added to the first ".". You're right. – Ted Hopp Jul 20 '12 at 07:54
1

From the beggining of line searhinng position any symbols before prodCode=. (?=) means just check of position not match. So in your situation if in the line exists string like any symbol + prodCode= then we match whole line if not then return false.

Sergii Stotskyi
  • 4,324
  • 1
  • 18
  • 19
1

This matches if the string has prodCode= anywhere in it and matches the complete string.

Another way of writing it (roughly, abusing a method return value as the regex matcch) would be

if (s.indexOf("prodCode=") != -1)
    return s;
Joey
  • 316,376
  • 76
  • 642
  • 652
  • so the pattern applied to `"XprodCode=9"` would return `"X"`? – posdef Jul 20 '12 at 07:46
  • No. I was basing my old answer on the mangled and ill-formatted regex in the original question. I have since revised it – Joey Jul 20 '12 at 08:04