How can I make the match fail if the first occurence of a pattern doesn't match a condition?

Aran-Fey

I have a comma-separated string of key=value pairs like this:

foo=1,foo=1,bar=2

In this string I want to capture the value of the first foo, but only if it's immediately followed by bar=2.

Examples:

  • In this string, the value 1 should be captured:

     baz=0,foo=1,bar=2,foo=3,bar=4
    
  • In this string, nothing should be captured:

     baz=0,foo=1,foo=1,bar=2
    

My current solution uses a tempered greedy token, but that forces me to duplicate the foo=[^,]*, part of the regex:

^(?:(?!foo=[^,]*,).)*foo=([^,]*),bar=2(?:,|$)

Is there any way to do this without having to duplicate such a big part of the regex?

melpomene

It's pretty easy with backtracking control verbs:

(?<![^,])foo=([^,]*)(*COMMIT),bar=2(?![^,])

We match a position not preceded by a non-comma character (i.e. the start of the string or immediately after ,), followed by foo=, followed by 0 or more non-comma characters (which we capture). This is the foo=... part.

We then commit to the first match found and require a ,bar=2 match, not followed by a non-comma character (i.e. a , or the end of the string).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to match the last occurence of a pattern?

How can I pattern match on this?

in bash, how can I find a pattern in one file that doesn't match any line of another file?

How can I make a substitution on the line of first occurrence of a match only?

pcregrep: how to match only the first occurence?

How can I get this regex match to fail?

How can I rename files only if they don't match a pattern?

With grep, how can I match a pattern and invert match another pattern?

How do I use positive and negative lookbehinds to match only if a certain pattern doesn't match before and after it?

Why can't I simplify this pattern match?

How to stop match at first occurence of a match on each line?

Regex: make 'zero or one time occurence' condition but only for the last match

How can I match something if it doesn't contain specific word?

How can I deserialize an enum when the case doesn't match?

How can I pattern match for "any string"?

How can i use OR condition in match?

"sed" doesn't match pattern

How to make elements first in a list that match a given condition?

Why doesn't VS Code Cpptools' GCC problem matcher match against linker errors, and how can I make it so?

How can I make this cases match with regex?

Can't pattern match on HttpMethod

How to match only first occurence of a string per line with regex

How can I match only the first N instances of a pattern, then print lines following each pattern until a blank line?

How to serve a file if URL doesn't match to any pattern in Go?

Why can't I pattern match against a ratio in Haskell?

Why can't I pattern match on a type family?

Why can't I pattern match on Stream.empty in Scala?

Why can't I pattern match on the concatenation function (++) in Haskell?

How can I make this select lookup another table and find first match?

TOP Ranking

HotTag

Archive