regex to match up to first occurrence of

偶尔善良 提交于 2019-12-14 01:55:01

问题


I have this text:

{{Infobox Item
|name = value
|prop2 = value
|prop3 = value
}}

it's from a mediawiki template.

i have the following regex:

preg_match('/\{\{Infobox Item.+\\n\}\}\\n/s', $text, $ra);

I'm using PHP.

I want to match the from {{Infobox Item up to the first occurrence of }} which is always going to be on a line by itself. The above regex will match from {{Infobox Item to the end of another {{ }} style block, which isn't what I want. How can I achieve this?


回答1:


Code

preg_match('/{{Infobox Item.+?^}}$/sm', $subject, $regs)

Regular Expression

{{Infobox Item.+?^}}$

https://regex101.com/r/gC0oV1/1

Human Readable

# {{Infobox Item.+?^}}$
# 
# Options: Case sensitive; Exact spacing; Dot matches line breaks; ^$ match at line breaks; Greedy quantifiers
# 
# Match the character string “{{Infobox Item” literally (case sensitive) «{{Infobox Item»
# Match any single character «.+?»
#    Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
# Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed) «^»
# Match the character string “}}” literally «}}»
# Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»


来源:https://stackoverflow.com/questions/34246170/regex-to-match-up-to-first-occurrence-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!