Why is my regex putting a character in a different capture group than what I am expecting?

Bryan A

I have the given strings:

bar"{foo}"bar
bar{foo}bar

I am trying to get both the first and third capture group to be just bar. The second capture group should be {foo} both with and without quotes for each match respectively.

I have the following regex:

(^.*)("?\{.*\}"?)(.*$)

With these results:

Match 1
Full match.  bar"{foo}"bar
Group 1.     bar"
Group 2.     {foo}"
Group 3.     bar

Match 2
Full match.  bar{foo}bar
Group 1.     bar
Group 2.     {foo}
Group 3.     bar

Why are the " characters not both in the second group? I do not understand why it would be in the first if I am specifically calling it out in the second group. Do I need to tell the first group to ignore it or use it as a right bound?

Bryan A

As I was finishing writing this question I realized the problem I had. I needed to use a reluctant quantifier so the first group would not grab the " before the next group had a shot.

Here is the small correction - I added .*? (reluctant) instead of .* (greedy):

(^.*?)("?\{.*\}"?)(.*$)

Good explanation here: Greedy vs. Reluctant vs. Possessive Quantifiers

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my loop not work as I am expecting?

Why is the following output giving the response instead of what I am expecting?

Why is my list different than what I appended?

Why am I not getting Regex group values

Why I am getting different output on HackerRank than the output of my IDE?

These C function signatures are different than I am used to seeing. What do these do and why is it written this way?

I need help figuring out why my regex does not match with what I am looking for

Why is the console not printing the characters i am expecting

Why am I getting a capture-of applicability error in my class?

Regex capture group with different quantifiers

Why is my Firebase CLI version different than what I just installed with npm?

when i am putting the while loop inside a function why is the answer coming different?

Breaking my code by putting Dictionaries in a list. What am I doing wrong?

In Bash, I am trying to upload a file to AWS S3. Why is AWS generating a different canonical request hash than my code?

Regex capture group ( ) within character set [ ]

How to validate the last character in a capture group with regex

iron-ajax in tandem with polymer-starter-kit misbehaves or something. It sends requests to a different url than the one I am expecting

Why is my Gradient Layer smaller than the UITextField I am applying it to?

I am trying to select in my regex only what is between `<` and `-`

Why am I not getting all my regex captures?

Why am I getting different results with my code?

Why does this regex capture the excluded character?

while i am extract the character from the word at that time why confidence values of the similar character in words are different?

Reading csv file in C, strtok not returning what I am expecting?

C++ std::unique is not showing what I am expecting from it

spaCy: Matcher end token offset not what I am expecting

Installed Gnome on Sputnik 16.04, not seeing what I am expecting

What am I doing wrong - syntax error unexpected '\n', expecting =>

Can I flash a different BIOS than what was shipped with my PC?

TOP Ranking

HotTag

Archive