Is it possible to combine these two regular expression expressions into one?

i have the following two regular expressions(in btw order).

1. ^~/buying/(.*)\?(.*)   => foo= group 1  baa= group 2.
2. ^~/buying/(.*)         => foo= group 1  baa= nothing/empty/null/baibai

What I'm trying to do is if the url has a question mark, then divide it into two groups. Otherwise, just throw all the things in the first group.

the reason why order is important to me is that if I switch them, then ?? regex will never run because expression # 2 (above) will catch everything.

So, can this be fixed?

NOTE. I tried using this website ** to help me debug / tune ... but I can't figure it out.

** I have no connection with this site.

+3
source share
3

:

^~/buying/([^?]*)\??(.*)

([^?]*) - , .
\?? - .
(.*) - - .

+5

, ():

^~/buying/([^\?]*)(?:\?(.*))?

?: , (, , ).

: - [^ \?], . ( , .)

+1

, , .

^~/buying/(.*)(\?(.*))?$

0

Source: https://habr.com/ru/post/1748286/


All Articles