Questions tagged [regex-greedy]

The greedy regex property causes the regex engine to repeat a regex token as often as possible. Only if that causes the entire regex to fail, give up the last iteration, and proceed with the remainder of the regex. The greedy regex tokens are `+`, `*`, `?` and the repetition using curly braces.

Example of Greediness

Using a regex to match an HTML tag the regular expression does not need to exclude any invalid use of sharp brackets. An HTML tag will be anything between sharp brackets.

If the test string is the following:

This is a <EM>first</EM> test.

With the <.+> patterh a expected match would be <EM> and when continuing after </EM>. But he regex will match <EM>first</EM>.

The reason is that the plus is a greedy token.

969 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
567
votes
8 answers

How can I make my match non greedy in vim?

I have a big HTML file that has lots of markup that looks like this:

stuff here

I'm trying to do a Vim search-and-replace…
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
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
502
votes
27 answers

Non greedy (reluctant) regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain. So from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trailing slash, it doesn't matter) I have…
Joel
  • 29,538
  • 35
  • 110
  • 138
417
votes
7 answers

Greedy vs. Reluctant vs. Possessive Qualifiers

I found this tutorial on regular expressions and while I intuitively understand what "greedy", "reluctant" and "possessive" qualifiers do, there seems to be a serious hole in my understanding. Specifically, in the following example: Enter your…
Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
283
votes
3 answers

How to make Regular expression into non-greedy?

I'm using jQuery. I have a string with a block of special characters (begin and end). I want get the text from that special characters block. I used a regular expression object for in-string finding. But how can I tell jQuery to find multiple…
Rueta
  • 3,317
  • 2
  • 23
  • 22
248
votes
7 answers

Python non-greedy regexes

How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"? I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is…
So8res
  • 9,856
  • 9
  • 56
  • 86
231
votes
7 answers

How to do a non-greedy match in grep?

I want to grep the shortest match and the pattern should be something like: ... ... ... ... means any character and the input is multiple lines.
syker
  • 10,912
  • 16
  • 56
  • 68
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
80
votes
5 answers

Question marks in regular expressions

I'm reading the regular expressions reference and I'm thinking about ? and ?? characters. Could you explain me with some examples their usefulness? I don't understand them enough. thank you
xralf
  • 3,312
  • 45
  • 129
  • 200
77
votes
15 answers

Regex credit card number tests

I'm testing one application where Regex pattern match credit card then such numbers should be highlighted. I'm using site http://regexpal.com/ to create test credit credit card numbers for my testing. my requirement is to have valid credit card…
user1209784
  • 791
  • 1
  • 6
  • 5
66
votes
4 answers

Notepad++ non-greedy regular expressions

Does Notepad++ support non-greedy regular expressions? For example for text: abcxadc I want to get parts using pattern: a.+c And now I get whole string instead of 2 parts. I tried to use the '?' operator but without success.
Przemysław Michalski
  • 9,627
  • 7
  • 31
  • 37
56
votes
4 answers

Why is `\s+` so much faster than `\s\s*` in this Perl regex?

Why does replacing \s* (or even \s\s*) with \s+ result in such a speedup for this input? use Benchmark qw(:all); $x=(" " x 100000) . "_\n"; $count = 100; timethese($count, { '/\s\s*\n/' => sub { $x =~ /\s\s*\n/ }, '/\s+\n/' => sub { $x =~…
rjh
  • 49,276
  • 4
  • 56
  • 63
32
votes
6 answers

Why are regular expressions greedy by default?

It seems that this is a huge source of confusion for beginners writing regular expressions, can cause hidden performance problems, and it would seem that a typical use case would be non-greedy. Is this just for legacy reasons (it was how it was…
Yishai
  • 90,445
  • 31
  • 189
  • 263
1
2 3
64 65