开发者

Regular expression to match all content between 2 tags

开发者 https://www.devze.com 2023-01-22 17:11 出处:网络
I am trying to capture the following content in PHP using regular expressions: /* Name: Test Description: my te开发者_运维知识库st

I am trying to capture the following content in PHP using regular expressions:

/*

Name: Test

Description: my te开发者_运维知识库st

*/

I have tried the code from here: Match everything inbetween two tags with Regular Expressions? but it doesnt capture the new lines.

EDIT: I used the following regular which works on a single line but it stops working as soon as it sees a line break

EDIT2: I want to include that I am running this script on a large piece of text which has a lot of line breaks. I am sure we need to use * because I am unsure of the number of line-break occurrences.

/*(.*?)*/

TIA


Ah ok. The dot . in regular expressions does not match newlines per default. You need the /s modifier (after the regex end):

 preg_match("#/[*](.*?)[*]/#s", ...

Also see above, you need to escape the * (with a backslash or surrounding []) and if you want to match / you also need another character to enclose it in. I've used # instead of /

When in doubt, always add /ims. It's no performance drain when you don't use ^ $ or . and letters anyway.


Use the flag s (s stands for dotall)

/*(.*?)*/s

This flag lets the dot also match newline-characters(without this flag it would'nt)

0

精彩评论

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

关注公众号