简体   繁体   English

正则表达式内部带有可选部分

[英]RegEx with optional part inside

I'm pretty new to regular expression. 我对正则表达式很陌生。 Just tried to analyse a "BB-Code" that could do something like this: 刚刚尝试分析“ BB代码”,它可能会执行以下操作:


Pattern: 图案:

\[element title=(.*)picture=(\d+)](.*)(?:\[caption](.*):?\[/caption])?\[/caption].*

Search: 搜索:

[element title=element title picture=32]Lorem ipsum dolor[caption]Photo by John Doe[/caption][/element] [元素标题=元素标题图片= 32] Lorem ipsum dolor [标题]照片由John Doe [/ caption] [/ element]

[element title=element title picture=32]Lorem ipsum dolor[/element] [元素标题=元素标题图片= 32] Lorem ipsum dolor [/ element]


Well, the caption-part should be optional and both entries should give results. 好吧,标题部分应该是可选的,并且两个条目都应给出结果。 How can I reach this? 我怎样才能做到这一点?

How about this: 这个怎么样:

\[element title=(.*)picture=(\d+)\](.*?)(\[caption\](.*)\[/caption\])?\[/element\]

It will match both: 它将同时匹配:

[element title=element title picture=32]Lorem ipsum dolor[caption]Photo by John Doe[/caption][/element]

[element title=element title picture=32]Lorem ipsum dolor[/element]

Example

in PHP, you can use it this way: 在PHP中,您可以通过以下方式使用它:

$regex = '#\[element title=(.*)picture=(\d+)\](.*?)(\[caption\](.*)\[/caption\])?\[/element\]#i';
$text = '[element title=element title picture=32]Lorem ipsum dolor[caption]Photo by John Doe[/caption][/element]';    

preg_match ( $regex, $text, $match );

print_r( $match );

The array $match will have several elements. 数组$match将包含几个元素。 Those are the strings that are surrounded by round-brackets ( and ) in the regular expression. 这些是在正则表达式中用圆括号()包围的字符串。 One of them is the caption text. 其中之一是字幕文本。

Program execution and output can be seen here http://ideone.com/vQ1T0 . 程序执行和输出可在此处http://ideone.com/vQ1T0看到。

\[element title=(.*) picture=[0-9]+\](.*)(\[caption\](.)*\[\caption\])?\[/element\]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM