Vinoth
Vinoth

Reputation: 123

Reg ex in Java for matching a pattern with end occurring in a new line

I need to match a specific content within a large text;

Ex to be matched:

{{Infobox....{{..}}..
...
..
..
.
}}

Issue here: The content might or might not contain a inner {{..}}. However I need to match till a specific closing double curly (}}) that occurs in a new line.

Reg Ex I have been trying:

1) String regex = "\\{\\{Infobox(.*?)\\}\\}"; //This matches till the first occurrence of }}

2) String regex = "\\{\\{Infobox(.*)\\}\\}";  //This matches till end of the file

Looking for some help to get the content till that specific closing curly brackets.

Upvotes: 2

Views: 111

Answers (2)

Josh T
Josh T

Reputation: 564

{{Infobox.*\n}}

You could try this, it works over at regexpal.com.

The "\n}}" matches "}}" with a linebreak right before it.

You need to make sure in your implementation that "." matches all, so that the "." will keep searching through line breaks.

Upvotes: 0

Robin Green
Robin Green

Reputation: 33093

I can see you are trying to parse Mediawiki wikitext with regular expressions. This can't be done. Regular expressions can't count curly braces, which you need to do because you can have any level of nested template invocations.

I also want to parse Mediawiki wikitext from Java, and I found this useful Stackoverflow question.

Upvotes: 1

Related Questions