how to exclude some url from a regex match

那年仲夏 提交于 2019-12-08 07:54:33

问题


I have 3 type payment ( online, offline, by balance) every payment type have different url so I'm using regex for match all this url :

1- order/checkout/unpaid/done

2- order/checkout/paid/done?Authority=000000000000000000000000000039067905&Status=OK 3- order/checkout/paid/done?Status=OK

regex : (.*?)done

this regex match all of urls but the problem is it match some page too ! kile this product page :

/tork-doner-motahari-tehran

What is the solution ?


回答1:


You may use

(.*)done($|[?])

The point is that you need to match done at the end of the string or at the last ?. The pattern will match

  • (.*) - any 0+ characters, as many as possible, up to the last occurrence of the subsequent subpatterns
  • done - a literal substring done
  • ($|[?]) - either $ (end of string anchor) or a literal ? (can be written as \? in the pattern, the main point being that ? without brackets or a \ in front means 1 or 0 repetitions of the atom to the left of the ?).

Note that you may turn the groups into non-capturing by adding ?: after ( if you are not using the capturing group values.



来源:https://stackoverflow.com/questions/44780839/how-to-exclude-some-url-from-a-regex-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!