开发者

Need Help in PHP Regex

开发者 https://www.devze.com 2023-02-04 16:39 出处:网络
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..

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 ?


\w represents 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 \w represents 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:

$pattern = "/-start-(.+)-end- /";

Supposing you might encounter an expression like: -start- foo -end- -end- and you want to terminate on the first -end- (the content to extract is foo), 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:

$pattern = "/-start-(.+?)-end- /";
0

精彩评论

暂无评论...
验证码 换一张
取 消