Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define switch cases within regex?

Tags:

java

regex

How can I make a single regex that does the following:

"in general, do a generic regex. But if specific characters follow each other, examine the string following these specific characters in a different way"?

one == two && three | four

The general regex would be: [&|=\s]+.

Which would result in when splitting: one, two, three, four.

But what if I want to apply a different regex everytime there is a =character, and want the expression coming after the = to stop only at the | character? So that I'd get the result: one, two && three, four.

How could I do this?

like image 513
membersound Avatar asked Mar 24 '23 08:03

membersound


1 Answers

Here is one possibility:

(?=[&|=\s])[&|\s]*(?:=[^|]*?[|][&|=\s]+)?

Or in free-spacing mode with explanation:

(?=[&|=\s])  # make sure there is at least a single separator character ahead;
             # this is important, otherwise you would get empty matches, and
             # depending on your implementation split every non-separator
             # character apart.
[&|\s]*      # same as before but leave out =
(?:          # start a (non-capturing) group; it will be optional and treat the
             # special =...| case
  =          # match a literal =
  [^|]*?     # match 0 or more non-| characters (as few as possible)
  [|]        # match a literal | ... this is the same as \|, but this is more
             # readable IMHO
  [&|=\s]+   # and all following separator characters
)?           # make the whole thing optional

Try it out.

EDIT:

I just realised, this swallows up the central part, but you want to return that as well. In that case, you might be better off with matching instead of splitting (using find). This pattern should do the trick:

=[&|=\s]*([^|]+?)[&|=\s]*[|]|([^&|=\s]+)

Now either the first or second capturing group will contain the desired result. Here is an explanation:

#this consists of two alternatives, the first one is the special case
  =            # match a literal =
  [&|=\s]*     # consume more separator characters
  ([^|]+?)     # match 1 or more non-| characters (as few as possible) and
               # capture (group 1)
  [&|=\s]*     # consume more separator characters
  [|]          # match a literal |
|              # OR
  ([^&|=\s]+)  # match 1 or more non-separator characters (as many as possible)
               # and capture (group 2)

Try it out.

like image 144
Martin Ender Avatar answered Mar 27 '23 11:03

Martin Ender