Matching double quotes
Moderator: General Moderators
Matching double quotes
All,
I am having a bit of trouble with a regex. What I need to do is match and opening and closing double quotes with any type of text in between. I have tried several different versions and they all seem to work except that it does not check for opening and closing quotes. It will match any quotes within a string instead of matching a pair and the text in between.
Any help greatly appreciated.
Jason
I am having a bit of trouble with a regex. What I need to do is match and opening and closing double quotes with any type of text in between. I have tried several different versions and they all seem to work except that it does not check for opening and closing quotes. It will match any quotes within a string instead of matching a pair and the text in between.
Any help greatly appreciated.
Jason
Code: Select all
/"([^"]*)"/Okay, now I get what you mean.
Code: Select all
/"((?:[^\\"]++|\\.)*)"/- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
I don't think this would work, GeertDD.
jbullard, try this pattern
jbullard, try this pattern
Code: Select all
'/"(\\\\.|[^"])*"/'Uh, mine works and yours doesn't, stereofrog. Have a look at it again.
Code: Select all
$str = 'Quote: "Spend \"more\", buy less".';
// GeertDD:
preg_match_all('/"((?:[^\\"]++|\\.)*)"/', $str, $matches);
/*
$matches = Array
(
[0] => Array
(
[0] => "Spend \"more\", buy less"
)
[1] => Array
(
[0] => Spend \"more\", buy less
)
)
*/
// stereofrog:
preg_match_all('/"(\\\\.|[^"])*"/', $str, $matches);
/*
$matches = Array
(
[0] => Array
(
[0] => "Spend \"
[1] => ", buy less"
)
[1] => Array
(
[0] => \
[1] => s
)
)
*/- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Okay, I see now. Mine worked because my little selfmade regex test app applied addslashes() to the posted regex. How bad. 
So mine really only works after adding the slashes manually. Try this:
And yours does work too, stereofrog. I'd just make the parentheses non-capturing.
So mine really only works after adding the slashes manually. Try this:
Code: Select all
/"((?:[^\\\\"]++|\\\\.)*)"/Thanks for the help guys. However, I ended taking your first example and adding stuff I already had which works perfectly with or without slashes.
This is a double quoted string with anything in between.
And this one is for single quoted strins with anything in between.
Okay so I only added the dot after ], but hey, it works right.
Now, here is another problem. Let's say I want to match a specific string inside of another string. So, I will say this:
Now, I know that I can use a regex to just match the word "is"; however, how do I prevent the following regex from matching "is" in "this".
Again, I have tried many different parameters and it always seems to match "is" in "this".
This is a double quoted string with anything in between.
Code: Select all
/"([^"].*)"/And this one is for single quoted strins with anything in between.
Code: Select all
/\'([^\'].*)\'/Okay so I only added the dot after ], but hey, it works right.
Now, here is another problem. Let's say I want to match a specific string inside of another string. So, I will say this:
Code: Select all
$mystring = "Welcome to this place";Code: Select all
$regex = '(is)';Note that that regex is almost the same asjbullard wrote:This is a double quoted string with anything in between.Code: Select all
/"([^"].*)"/
Code: Select all
/".*"/You could use \b (word boundary) around the word.Now, I know that I can use a regex to just match the word "is"; however, how do I prevent the following regex from matching "is" in "this".Code: Select all
$mystring = "Welcome to this place";
Again, I have tried many different parameters and it always seems to match "is" in "this".Code: Select all
$regex = '(is)';
Code: Select all
/\bis\b/- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
It actually doesn't. Study the output from the following example and try to find out why your expression doesn't print the correct resultjbullard wrote:
Okay so I only added the dot after ], but hey, it works right.![]()
Code: Select all
$str = ' begin "one" and "two" and "" and "esc " ape" end ';
$re = '/"([^"].*)"/';
preg_match_all($re, $str, $m);
print_r($m);
$re = '/"(\\\\.|[^"])*"/';
preg_match_all($re, $str, $m);
print_r($m);