How can I write a regex which matches non greedy? [duplicate]
Asked Answered
H

3

522

I need help about regular expression matching with non-greedy option.

The match pattern is:

<img\s.*>

The text to match is:

<html>
<img src="test">
abc
<img
  src="a" src='a' a=b>
</html>

I test on http://regexpal.com

This expression matches all text from <img to last >. I need it to match with the first encountered > after the initial <img, so here I'd need to get two matches instead of the one that I get.

I tried all combinations of non-greedy ?, with no success.

Hamner answered 10/8, 2012 at 9:37 Comment(2)
What language are you running the REGEX in?Rosario
The ? operand makes match non-greedy. I don't know where is the issue. check this demo.Misreckon
K
736

The non-greedy ? works perfectly fine. It's just that you need to select dot matches all option in the regex engines (regexpal, the engine you used, also has this option) you are testing with. This is because, regex engines generally don't match line breaks when you use .. You need to tell them explicitly that you want to match line-breaks too with .

For example,

<img\s.*?>

works fine!

Check the results here.

Also, read about how dot behaves in various regex flavours.

Klute answered 10/8, 2012 at 9:42 Comment(6)
There is also a trick you can do to work around this: Since \s means "any whitespace", and "\S" means "any non-whitespace", [\s\S] will match ANY character (like ".", but including new line)! Similarly, you could use [\d\D], or [\w\W]. This can be quite a handy little "hack", and it certainly a very useful trick to be aware of.Slim
Or even, in this example, you could use: <img[^>]*> to achieve the same affect: since "Any character other than >" DOES include new line!Slim
good answer, but how about bash? echo "<img src=test>bla<img src=a>" | grep -P '<img\s.*?>' matches the whole string despite the ? operator.Winger
@Thorsten: -P selects Perl mode and perldoc says *? is non-greedy. Confirmed to work on a 10-year-old Linux and a recent Linux. Maybe you misinterpreted the output. "grep" prints any line (in full) that has a match somewhere. Add "-o" to only print the matches.Giblet
I intend to find the pattern in the line below. line = "/ab[1].bc[2].cd[3]"; pattern="([a-zA-Z0-9].*?\[\\d*?\])"; I can find multiple matches in TextFX,notepad++ but in java it finds only 1 matchBeachcomber
There is also some wise guy telling not to use regular expressions for HTML parsing :). Check -> #1732848Remunerate
M
156

The ? operand makes match non-greedy. E.g. .* is greedy while .*? isn't. So you can use something like <img.*?> to match the whole tag. Or <img[^>]*>.

But remember that the whole set of HTML can't be actually parsed with regular expressions.

Myrmecology answered 10/8, 2012 at 9:43 Comment(2)
Your answer reminded of this: https://mcmap.net/q/17499/-regex-match-open-tags-except-xhtml-self-contained-tagsOvariotomy
I think It's more clear to say that *? is the non-greedy version of *.Unsought
N
40

The other answers here presuppose that you have a regex engine which supports non-greedy matching, which is an extension introduced in Perl 5 and widely copied to other modern languages; but it is by no means ubiquitous.

Many older or more conservative languages and editors only support traditional regular expressions, which have no mechanism for controlling greediness of the repetition operator * - it always matches the longest possible string.

The trick then is to limit what it's allowed to match in the first place. Instead of .* you seem to be looking for

[^>]*

which still matches as many of something as possible; but the something is not just . "any character", but instead "any character which isn't >".

Depending on your application, you may or may not want to enable an option to permit "any character" to include newlines.

Even if your regular expression engine supports non-greedy matching, it's better to spell out what you actually mean. If this is what you mean, you should probably say this, instead of rely on non-greedy matching to (hopefully, probably) Do What I Mean.

For example, a regular expression with a trailing context after the wildcard like .*?><br/> will jump over any nested > until it finds the trailing context (here, ><br/>) even if that requires straddling multiple > instances and newlines if you let it, where [^>]*><br/> (or even [^\n>]*><br/> if you have to explicitly disallow newline) obviously can't and won't do that.

Of course, this is still not what you want if you need to cope with <img title="quoted string with > in it" src="other attributes"> and perhaps <img title="nested tags">, but at that point, you should finally give up on using regular expressions for this like we all told you in the first place.

Neckband answered 19/11, 2018 at 5:50 Comment(1)
(><br/> is obviously pronounced "BR Fish")Neckband

© 2022 - 2024 — McMap. All rights reserved.