Reputation: 12002
I am trying to search a string for a pattern in the format
{{ Control :: 123 }}
or
{{Control::123}}
or
{{ Control ::123 }}
or
{{Control :: 123}}
Basically the space can be anywhere but it will have to start with the double open curly brackets, followed for the word "Control" followed by :: followed by a number ending with double closing curly brackets.
Here is what I have done
$pattern = '/{{ ?% ?Control ?:: ?([1-9]+([0-9]{1,2})?) ?% ?}}/i';
$subject = 'This is my subject for "{{ Control:: 132}}" ';
$matches = [];
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
I need to be able to return the found pattern i.e {{ Control:: 132}}
. Then I need to be able to find the number 132
What am I doing wrong here for this not to work?
Upvotes: 0
Views: 29
Reputation: 4482
Your regex doesn't work because you didn't escape "{" and "}".Additionally you can write it more simply.
Try this:
$pattern = '/\{\{\s?Control\s?::\s?(\d+)\s?\}\}/i';
Note that it might be enhanced to accept more than 1 space everywhere:
$pattern = '/\{\{\s*Control\s*::\s*(\d+)\s*\}\}/i';
Upvotes: 2