399

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Pointer Null
  • 36,993
  • 13
  • 79
  • 106

3 Answers3

560

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Pavan Manjunath
  • 24,688
  • 11
  • 92
  • 120
  • 37
    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. – Tom Lord Nov 21 '14 at 11:45
  • 10
    Or even, in this example, you could use: `]*>` to achieve the same affect: since "Any character other than `>`" DOES include new line! – Tom Lord Nov 21 '14 at 11:52
  • 1
    good answer, but how about bash? echo "bla" | grep -P '' matches the whole string despite the ? operator. – Thorsten Staerk Mar 22 '15 at 08:47
  • @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. – Joachim Wagner Jan 21 '16 at 08:54
  • 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 match – Mrinal Bhattacharjee Mar 17 '16 at 14:29
102

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ilya
  • 1,464
  • 1
  • 10
  • 8
20

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.

tripleee
  • 139,311
  • 24
  • 207
  • 268