Log in

View Full Version : PHP Regex to find text in quotes with a line break



rwilkin
01-29-2011, 10:56 PM
Hi All,

I have this regex pattern to find text in quotation marks - '/named([\s]|[\r\n])"([^"]+)"/i'


the strings I am looking for always have 'named ' before the "name" part. This works for most of the file I search through. However, sometimes the file I get has a line break between named and "name" and sometimes it has a line break and a tab between them.
i.e
named "name 1" -- Works

named
"name 2" -- Doesn't work

named
[tab]"name 3" -- Doesn't work

Can anyone tell me what I am doing wrong?

james438
01-30-2011, 02:46 AM
well, with '/named([\s]|[\r\n])"([^"]+)"/i' you are looking for "named " followed by either a whitespace or a \r\n which is also a type of whitespace, but not both together, such as \s\r\n. In this case, the best solution is to not worry about the different types or combinations of whitespace that you may use and just use '/named\s*?"([^"]+)"/i'. The \s will match any type of whitespace whether it be tab, carriage return, newline, or space. *? in this case says that the immediately preceding character can be repeated as little as 0 times or as many as it takes until it reaches the first available double quote.

rwilkin
01-31-2011, 06:51 AM
Fantastic!
Thank you so much james438 :D I've spent the better of 3 days trying to get my head around this :)