I am studying about regex, i figured out some about matching one or more character, but i have a case, but don’t know how to solve this..
For example i have:
$data = "bla bla -start- blu blu blu -end- bla bla";
$pattern = "/\-start\-[\w]\-end\- /";
preg_match($pattern, $data, $matches);
print_r($matches);
i intend to take anything between ‘-start-‘ and ‘-end-‘, so i expect to get
‘ blu blu blu ‘.
any suggestion ?
\wrepresents only word characters, and you need to also allow for spaces. Assuming you really want to allow anything in between-start-and-end-you can use.which matches any character.Hyphens need not be escaped unless enclosed in a character pattern (between square brackets
[and]), so you can replace\-with just-.Just like using a single
\wrepresents matching any single word character,.represents matching any single character, so you need to add some more information in. Following either of these with+would indicate matching at least one character, or with a*would indicate zero or more characters. Assuming you want at least one character, your expression should be okay like this:Supposing you might encounter an expression like:
-start- foo -end- -end-and you want to terminate on the first-end-(the content to extract isfoo), then you need to operate in a non-greedy way. PHP’s regex is greedy by default, to turn this off, you follow the+(or a*) with a?, like this: