Questions tagged [non-greedy]

A technique used in regular expressions, that limits the matching text until all conditions of the given regex have been met. The operator "?" is added to the end of wildcard operations.

A regex is used to check if a string matches a certain pattern. Most regexes offer additional functionality to capture interesting parts of the string.

Example:

Say we have the following regular expression:

^(.*)([ab]+)$

The regex specifies a pattern: strings can start with any sequence of arbitrary characters, but should end with at least one a or b.

Wildcard operations are by default greedy. This means that the first group will aim to capture as much as possible (without losing the match) and only give up the remainder of the string if this is the only way to match the string with the pattern.

For instance the string foobaraabbabbababab will be captured as (foobaraabbabbababa)(b). In case we more interested in the ([ab]+), group, we can apply a non-greedy operator on the first group such that the remainder of the string is passed to the second group as soon as possible.

In case we use the following pattern:

^(.*?)([ab]+)$

The example will be matched as (foobar)(aabbabbababab)

Related tags:

188 questions
779
votes
13 answers

What do 'lazy' and 'greedy' mean in the context of regular expressions?

What are these two terms in an understandable way?
ajsie
  • 77,632
  • 106
  • 276
  • 381
504
votes
3 answers

How can I write a regex which matches non greedy?

I need help about regular expression matching with non-greedy option. The match pattern is: The text to match is: abc I test on http://regexpal.com This expression matches all…
Pointer Null
  • 39,597
  • 13
  • 90
  • 111
217
votes
3 answers

What is the difference between .*? and .* regular expressions?

I'm trying to split up a string into two parts using regex. The string is formatted as follows: text to extract I've been using (.*?)< and <(.*?)> which work fine but after reading into regex a little, I've just started to wonder why I need…
Doug
  • 2,972
  • 3
  • 22
  • 28
170
votes
3 answers

RegEx: Smallest possible match or nongreedy match

How do I tell RegEx (.NET version) to get the smallest valid match instead of the largest?
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
37
votes
3 answers

Regex Non-Greedy (Lazy)

I'm attempting to non-greedily parse out TD tags. I'm starting with something like this: stuffMore stuff
steventnorris
  • 5,656
  • 23
  • 93
  • 174
31
votes
2 answers

Is there a way to use ungreedy matching in JavaScript for regular expressions?

I wonder if there is a way to use ungreedy matching in JavaScript? I tried the U modifer, but it doesn't seem to work. I want to write a small BBCode parser in JavaScript, but without ungreedy matching it isn't possible (at least as far as I see it)…
okoman
  • 5,529
  • 11
  • 41
  • 45
25
votes
3 answers

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

How can I get all the matches in the following example: // Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*"); // Only "ab" is matched MatchCollection lazyMatches = Regex.Matches("abcd", @"ab.*?"); // How can I…
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
17
votes
2 answers

Non-greedy Regular Expression in Java

I have next code: public static void createTokens(){ String test = "test is a word word word word big small"; Matcher mtch = Pattern.compile("test is a (\\s*.+?\\s*) word (\\s*.+?\\s*)").matcher(test); while (mtch.find()){ for…
Divers
  • 9,531
  • 7
  • 45
  • 88
15
votes
3 answers

non-greedy multiline search in vim

I am trying to search ruby file and find all methods (before autoreplacing them later). In vim, i use following regexp: /\vdef.*(\n.*){-}end However even though i use "{-}", it selects whole file's contents.
Pavel K.
  • 6,697
  • 8
  • 49
  • 80
14
votes
1 answer

non-greedy matching in Scala RegexParsers

Suppose I'm writing a rudimentary SQL parser in Scala. I have the following: class Arith extends RegexParsers { def selectstatement: Parser[Any] = selectclause ~ fromclause def selectclause: Parser[Any] = "(?i)SELECT".r ~ tokens def…
Magnus
  • 10,736
  • 5
  • 44
  • 57
13
votes
4 answers

Make a non-greedy RegEx in backward direction to behave the same like in forward direction

This pattern: /a+?b+?/ Against the following string: aaaaaabbbbbb Matches: aaaaaab We see that the non-greedy behaves different in backward/left direction (takes all) and forward/right direction (takes just one). Is there a way to make the…
flori
  • 14,339
  • 4
  • 56
  • 63
12
votes
6 answers

Regex: Is Lazy Worse?

I have always written regexes like this ([^<]*) but I just learned about this lazy thing and that I can write it like this (.*?) is there any disadvantage to using this…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
12
votes
3 answers

Regex is behaving lazy, should be greedy

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code: Regex keywords = new Regex(@"in|int|into|internal|interface"); var targets = keywords.ToString().Split('|'); foreach (string t…
Stomp
  • 890
  • 1
  • 6
  • 19
11
votes
4 answers

Why does my non-greedy Perl regex still match too much?

Say, I have a line that contains the following string: "$tom" said blah blah blash. "$dick" said "blah blah blah". "$harry" said blah blah blah. and I want to extract "$dick" said "blah blah blah" I have the following code: my ($term) =…
Mike
  • 1,841
  • 5
  • 24
  • 34
10
votes
5 answers

Posix regular expression non-greedy

Is there a way to use a non-greedy regular expression in C like one can use in Perl? I tried several things, but it's actually not working. I'm currently using this regex that matches an IP address and the corresponding HTTP request, but it's greedy…
user2212190
  • 357
  • 1
  • 6
  • 17
1
2 3
12 13