As you can see, the difference is the use of the greedy pattern .* In the second regular expression after " without space."
The reason why it behaves differently is because after no_but_why= there is a double quote, and .* Is a greedy pattern that matches up to the last " before /> in the second regular expression.
In your first regular expression, "\(.*\)" "bob3" only "bob3" , because after that there is a space due to which the regex mechanism prevents .* From matching up to the last double quote in the input.
To avoid this situation , you should use a negative character class instead of greedy matching.
Consider these examples of sed commands:
sed -e 's/.*value="\([^"]*\)" .*/\1/' <<< "$abcs" bob3 sed -e 's/.*value="\([^"]*\)".*/\1/' <<< "$abcs" bob3
Now you can see that both commands produce the same output of bob3 , because the negative character class [^"]* will match until it becomes the next " not until the very last " case with .* .
source share