Cjmarkham
Cjmarkham

Reputation: 9681

PHP regex match only one double quote

Im trying to match a line of code which only has one double quote ("). I have tried:

\"{1}

on the following strings

"this is a string"
"this is a string

the regex should only match the second line but it matches both of them.

Anyone know how to do this?

Upvotes: 0

Views: 1028

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

If you want to find out if there is only one double-quote character on any given line, try this:

/^[^"]*"[^"]*$/m

Note the m modifier - this allows ^ and $ to match the start and end of any given line, as opposed to just the start and end of the matched string.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109567

You are looking for the not group.

^[^\"]*\"[^\"]*$

Upvotes: 4

Related Questions