tags:

views:

127

answers:

5

I need some help with some regular expression problems I am having. First off, double quotes ".

Anything between "" needs to be matched. Next problem I need to match anything that starts with a ' till the end of a line \n or <br />.

I've tried all sorts, but nothing seems to match it. Any ideas?

Sorry guys, just realised I need the quotes to be between &quot; and &quot;.

+2  A: 

Simplistic:

"[^"]*"

will match anything between double quotes, though it won't work with escaped double quotes such as

"Abc\"Def"

For the single-quote to EOL, you can use

'.*$

Update: Sylverdrag's made a valid point; to match between the quotes you'd need

"([^"]*)"

and then get the first subgroup of the match. I see the question's been updated to mention that &quot; should be used - my answer can be adapted to this easily enough.

Vinay Sajip
Ah sorry, "
James Brooks
This will also match the quotes, though.
Sylverdrag
Better, but "([^"]*)" is greedy by default. You must add a ? to get the result you want: "([^"]*?)"
Sylverdrag
A: 

Try:

Double quotes:

\&quot\;(.*?)\&quot\;

Single quote till end of line or <br/>:

\'(.*)(\<br|$)
Nosrama
i think you should use (.*?) for the first (lazy mode) - that way it won't take in any other quotes
Dror
Good idea - edited.
Nosrama
A: 

anything between quotes

/"(.*)"/

from ' till end of line

/'(.*)$/
Charles Ma
A: 

Try these regular expressions:

/&quot;(.*?)&quot;/s
/'.*?(?=\n|<br \/>)/m
Gumbo
A: 

To get a bit more funky and match only what is inside the double quotes but not the quotes themselves:

(?<=").*?(?=")
Sylverdrag
I prefer the approach I gave in my updated answer.
Vinay Sajip
Fair enough, but make it ungreedy or there may be some surprises.
Sylverdrag