3

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

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

What's i'm trying to do is, if the url has a questionmark, then split it into two groups. Otherwise, just throw all the stuff into the first group.

the reason why the order is important for me, is that if i switch them round, the '?' regex will never get fired because the #2 expression (above) will catch all.

So .. can this be re-fixed?

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

** I have no affiliation with that site.

Community
  • 1
  • 1
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

3 Answers3

5

try this:

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

([^?]*) - Captures the first group, which doesn't contain a question mark.
\?? - Optional question mark.
(.*) - Optional second group - can be empty.

Kobi
  • 135,331
  • 41
  • 252
  • 292
1

How about just making the second part optional (untested):

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

The ?: is there to prevent the optional part (the part you need plus the question mark) from capturing.

Edit: Because of greediness, you need to use either the negated character class [^\?] or make it lazy. (The negated character class is preferred because it reduces backtracking.)

lc.
  • 113,939
  • 20
  • 158
  • 187
  • I've started with something similar. It doesn't work, because `.*` captures everything right from the start, leaving the second group empty. – Kobi Jun 03 '10 at 06:34
  • You'd need to add a "?" directly after the .* to make it non-greedy. – annakata Jun 03 '10 at 06:38
  • @annakata - you also need to add `$` at the end - or both groups will always be empty! – Kobi Jun 03 '10 at 06:39
  • @Kobi Yes, good point. And after that, yours is a bit easier to read as well. – lc. Jun 03 '10 at 06:42
0

also you can join like this, easier to understand.

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

Lukas Šalkauskas
  • 14,191
  • 20
  • 61
  • 77
  • Doesn't work - see my comment to LC. You can go non-greedy and add `$` at the end, but then it isn't so easier to understand anymore :) – Kobi Jun 03 '10 at 06:37