Michael
Michael

Reputation: 3238

RegEx problems with double quotes

I´m using the following RegEx to divide a string in keys and values:

String:

type="post" id="1" text="Blog Post"

Regex:

/(?<name>\\S+)=["']?(?P<value>(?:.(?!["']?\\s+(?:\\S+)=|[>"']))+.)["']?/u

That works fine as long as each value has at minimum 2 characters. I the value is only 1 character long (like id here) I get a quote after the number. So for this example with ID I get the following output:

type => post
id => 1"
text => Blog Post

I´ve no idea where my bug in the RegEx is. Maybe somebody can give me a hint?

Thanks a lot!

Upvotes: 1

Views: 46

Answers (1)

TarasovSA
TarasovSA

Reputation: 21

I think the latest point is redundant. Could you try this:

/(?<name>\\S+)=["']?(?P<value>(?:.(?!["']?\\s+(?:\\S+)=|[>"']))+)["']?/u

I don't konw how you are using this regex, but I think you can use this option:

/(\w+)=["']?([^"']+)["']/ug

Or provide me more info.

Upvotes: 1

Related Questions