2

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?

Stephen K
  • 697
  • 9
  • 26
  • See [How do you extract information from a Wikipedia infobox?](http://stackoverflow.com/q/33862336/323407) on the general applicability of regular expressions to infoboxes. – Tgr Dec 13 '15 at 07:34

1 Answers1

1

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) «$»
Dean Taylor
  • 40,514
  • 3
  • 31
  • 50
  • the }} isn't the last line in the file, which was my main problem. there is other content in the file as well as additional `{{Infobox whatever }}` style blocks. I just want the first of those blocks. – Stephen K Dec 12 '15 at 23:33
  • This doesn't require `}}` to be on the last line. – Dean Taylor Dec 12 '15 at 23:34
  • Note the use of the `/m` qualifier - changing the meaning of `^` and `$`. – Dean Taylor Dec 12 '15 at 23:38
  • nvm dean, that worked. it was my other code that was the problem. I was exiting if I found a line that did not have "prop = val".. there was a line with just `|prop` on it causing things to break.. sorry – Stephen K Dec 12 '15 at 23:39