How to identify switching cases in a regular expression?

How can I create one regex that does the following:

"in general, create a common regex. But if specific characters follow each other, look at the line following these specific characters differently?"

one == two && three | four 

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

This will lead to decay: one, two, three, four .

But what if I want to apply another regular expression every time there is a = character, and you want the expression that appears after = to stop only on the | character ? To get the result: one, two && three, four .

How can i do this?

+4
source share
1 answer

Here is one of the possibilities:

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

Or in free run mode with an 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 

Give it a try.

EDIT:

I just realized it’s absorbing the central part, but you also want to get it back. In this case, you might be better off with a match instead of splitting (using find ). This template should do the trick:

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

Now either the first or second capture 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) 

Give it a try.

+7
source

All Articles