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
7
votes
3 answers

Grammar a bit too greedy in Perl6

I am having problems with this mini-grammar, which tries to match markdown-like header constructs. role Like-a-word { regex like-a-word { \S+ } } role Span does Like-a-word { regex span { [\s+ ]* } } grammar…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
7
votes
1 answer

Mod Rewrite Regex Negative Lookahead

I am trying to match all URIs that begin with #/tool_[a-z\-]+# except if it's followed by /public. Such as /tool_calculator or whatever. For example, if the URI begins with /tool_store-front or /tool_store-front/anything-but-public then I want to…
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
7
votes
1 answer

Difference between exact greedy/reluctant X{n}?

In the documentation for the Java Pattern class, I see that the exact quantifier X{n} has both greedy and reluctant forms: Greedy quantifiers X{n} X, exactly n times ... Reluctant quantifiers X{n}? X, exactly n times ... The documentation…
Owen
  • 38,836
  • 14
  • 95
  • 125
7
votes
2 answers

Shortest match in regex from end

Given an input string fooxxxxxxfooxxxboo I am trying to write a regex that matches fooxxxboo i.e. starting from the second foo till the last boo. I tried the following foo.*?boo matches the complete string fooxxxxxxfooxxxboo foo.*boo also matches…
Srinath Mandava
  • 3,384
  • 2
  • 24
  • 37
7
votes
5 answers

Regular Expression nongreedy is greedy

I have the following text tooooooooooooon According to this book I'm reading, when the ? follows after any quantifier, it becomes non greedy. My regex to*?n is still returning tooooooooooooon. It should return ton shouldn't it? Any idea why?
Razor
  • 17,271
  • 25
  • 91
  • 138
7
votes
1 answer

Non greedy regex match, JavaScript and ASP

I need to do a non greedy match and hope someone can help me. I have the following, and I am using JavaScript and ASP match(/\href=".*?\/pdf\/.*?\.pdf/) The above match, matches the first start of an href tag. I need it to only match the last href…
Gerald Ferreira
  • 1,349
  • 2
  • 23
  • 44
7
votes
2 answers

PHP preg_replace non-greedy trouble

I've been using the following site to test a PHP regex so I don't have to constantly upload: http://www.spaweditor.com/scripts/regex/index.php I'm using the following regex: /(.*?)\.{3}/ on the following string (replacing with…
Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54
7
votes
1 answer

Multiline python regex

I have a file structured like this : A: some text B: more text even more text on several lines A: and we start again B: more text more multiline text I'm trying to find the regex that will split my file like this…
jmague
  • 572
  • 1
  • 6
  • 13
6
votes
5 answers

regex greedy problem (C#)

I've a input string like "===text=== and ===text===" and I want to replace wiki syntax with the corresponding html tag. input: ===text=== and ===text=== desirable output:

text

and

text

but with the following code I get this…
dannyyy
  • 1,784
  • 2
  • 19
  • 43
6
votes
2 answers

Python re.sub use non-greedy mode (.*?) with end of string ($) it comes greedy!

Code: str = '

A
B' print(re.sub(r'\w$', '', str)) It is expected to return

A, but it returns an empty string ''! Any suggestion?
Jet Guo
  • 243
  • 2
  • 6
6
votes
1 answer

Using regex replace in SSMS 2016 to trim lines

How can I use SSMS 2016 regex replace feature to remove extra spaces and tabs at the end of lines? Example of editor content: select 'tab' , 'space' select 'tabs' , 'spaces' Goal: select 'tab' , 'space' select …
jumxozizi
  • 642
  • 10
  • 21
6
votes
1 answer

Why does a space cause the remembered pattern in sed to output different things

I'm trying to get the value of the value entry in this xml line via terminal so I'm using sed. abcs='' echo $abcs | sed -e 's/.*value="\(.*\)" .*/\1/' echo $abcs | sed -e…
rasen58
  • 4,672
  • 8
  • 39
  • 74
6
votes
4 answers

Is it better to use a non-greedy qualifier or a lookahead?

I have a possibly large block of text to search for instances of [[...]], where the ... can be anything, including other brackets (though they cannot be nested; the first instance of ]] after [[ ends the match). I can think of two ways to match this…
Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
6
votes
2 answers

Make one or zero regex operator greedy

I have two sentences as input. Let's say for example: I love my red car. I love my car. Now I want to match every textpart inside the span-tags AND if available the color. If I use the following…
netblognet
  • 1,951
  • 2
  • 20
  • 46
5
votes
1 answer

Why is the regex quantifier {n,} more greedy than + (in Python)?

I tried to use regexes for finding max-length sequences formed from repeated doubled letters, like AABB in the string xAAABBBBy. As described in the official documentation: The '*', '+', and '?' quantifiers are all greedy; they match as much text…
Anton Ganichev
  • 2,184
  • 1
  • 18
  • 17
1 2
3
64 65