Reputation:
what is wrong with my preg_match ?
preg_match('numVar("XYZ-(.*)");',$var,$results);
I want to get all the CONTENT from here:
numVar("XYZ-CONTENT");
Thank you for any help!
Upvotes: 3
Views: 1894
Reputation: 226376
Paste your example string into http://txt2re.com and look at the PHP result.
It will show that you need to escape characters that have special meaning to the regex engine (such as the parentheses).
Upvotes: 1
Reputation: 31631
I assume this is PHP? If so there are three problems with your code.
/
, but you can use any matching pair you want.(
character but creating a RE group.numVar("XYZ-CONTENT1");numVar("XYZ-CONTENT2");
will match both, and your "content" group will be CONTENT1");numVar("XYZ-CONTENT2
.Try this:
$var = 'numVar("XYZ-CONTENT");';
preg_match('/numVar\("XYZ-(.*?)"\);/',$var,$results);
var_dump($results);
Upvotes: 2
Reputation: 3188
preg_match("/XYZ\-(.+)\b/", $string, $result);
print_r($result[0]); // full matches ie XYZ-CONTENT
print_r($result[1]); // matches in the first paren set (.*)
Upvotes: 0
Reputation: 1901
You should escape some chars:
preg_match('numVar\("XYZ-(.*)"\);',$var,$results);
Upvotes: 0