regex to match up to first occurrence of

2019-07-09 22:09发布

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条回答
We Are One
2楼-- · 2019-07-09 22:51

Code

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

Regular Expression

{{Infobox Item.+?^}}$

Regular expression visualization

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) «$»
查看更多
登录 后发表回答