0

Hi I am using a regex checker regex101.com to verify that my regex pattern is working. What I dont understand is why the position of my pattern

\/checkout\/index.jsp\?announceEmpty.*

does not work when it is at the end of my pattern match.

This regex DOES NOT work

\/checkout\/index.jsp.skipReprice=true|\/checkout\/index.jsp|\/checkout\/index.jsp\?announceEmpty.*

vs. this DOES work

\/checkout\/index.jsp.skipReprice=true|\/checkout\/index.jsp\?announceEmpty.*|\/checkout\/index.jsp

and these are the texts to match against

/checkout/index.jsp?skipReprice=true
/checkout/index.jsp
/checkout/index.jsp?announceEmpty=1&lastItemRemoved=Gola%20x%20
Cuong Vo
  • 249
  • 1
  • 6
  • 16
  • 1
    thank you @Wiktor Stribiżew – Cuong Vo Mar 17 '20 at 15:57
  • You are welcome, [this](https://stackoverflow.com/a/35606463/3832970) should be helpful. – Wiktor Stribiżew Mar 17 '20 at 16:01
  • Note that the regex you says works matches `/checkout/index.jsp?skipReprice=true my dog has fleas`. I don't know if that is intended. Depending on requirements and the regex engine you use, you may want to add an end-of-line anchor `$` or end-of-string anchor `\z` at the end. If you did so it would no longer match the string I mentioned. – Cary Swoveland Mar 17 '20 at 21:12
  • 1
    Rather than "or-ing" regexes that all start at the beginning of the string, you want to work left-to-right, only use alternation when needed, often within a non-capture group (`(?:...|....|....)`). For example, `/\/checkout\/index\.jsp\??(?:skipReprice=true|announceEmpty=.*%\d+)?$/`. See [PCRE engine Demo](https://regex101.com/r/I8UbEf/1/). – Cary Swoveland Mar 17 '20 at 21:42

0 Answers0