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
9
votes
2 answers

Can I have a non-greedy regex with dotall?

I would like to match dotall and non-greedy. This is what I have: img(.*?)(onmouseover)+?(.*?)a However, this is not being non-greedy. This data is not matching as I expected: siteItem
Rich Tier
  • 9,021
  • 10
  • 48
  • 71
8
votes
1 answer

Can regexes containing nongreedy (reluctant) quantifiers be rewritten to use only greedy ones?

Suppose I have a regex language supporting literals, positive and negative character classes, ordered alternation, and the greedy quantifiers ?, *, and +. (This is essentially a subset of PCRE without backreferences, look-around assertions, or some…
uckelman
  • 25,298
  • 8
  • 64
  • 82
8
votes
3 answers

Sed replace at second occurrence

I want to remove a pattern with sed, only at second occurence. Here is what I want, remove a pattern but on second occurrence. What's in the…
BeGreen
  • 765
  • 1
  • 13
  • 39
7
votes
6 answers

Why is the minimal (non-greedy) match affected by the end of string character '$'?

EDIT: remove original example because it provoked ancillary answers. also fixed the title. The question is why the presence of the "$" in the regular expression effects the greedyness of the expression: Here is a simpler example: >>> import re >>>…
krumpelstiltskin
  • 486
  • 7
  • 17
7
votes
2 answers

Which would be better non-greedy regex or negated character class?

I need to match @anything_here@ from a string @anything_here@dhhhd@shdjhjs@. So I'd used following regex. ^@.*?@ or ^@[^@]*@ Both way it's work but I would like to know which one would be a better solution. Regex with non-greedy repetition or…
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
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
6
votes
3 answers

RegExp in PHP. Get text between first level parentheses

I have two type of strings in one text: a(bc)de(fg)h a(bcd(ef)g)h I need to get text between first level parentheses. In my example this is: bc fg bcd(ef)g I tried to use next regular expression /\((.+)\)/ with Ungreedy (U)…
andreyb1990
  • 163
  • 1
  • 8
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
6
votes
4 answers

Non-greedy regex quantifier gives greedy result

I have a .net regex which I am testing using Windows Powershell. The output is as follows: > [System.Text.RegularExpressions.Regex]::Match("aaa aaa bbb", "aaa.*?bbb") Groups : {aaa aaa bbb} Success : True Captures : {aaa aaa bbb} Index :…
Dominic Cronin
  • 6,062
  • 2
  • 23
  • 56
5
votes
4 answers

Perl non-greedy

I am having a problem with a non-greedy regular expression (regex). I've seen that there are questions regarding non-greedy regex, but they don't answer my problem. Problem: I am trying to match the href of the "lol" anchor. Note: I know this can be…
vkats
  • 117
  • 1
  • 8
5
votes
1 answer

C++11 RegEx, non-greedy

I have a little bit of a problem with a C++11 RegEx and I think it is about greedynes. Here is a little sample. #include #include #include int main (void) { std::string in="{ab}{cd}[ef]{gh}[ij][kl]"; // the…
chris01
  • 10,921
  • 9
  • 54
  • 93
5
votes
4 answers

PHP preg_replace to turn **xyz** to xyz

I decided to, for fun, make something similar to markdown. With my small experiences with Regular Expressions in the past, I know how extremely powerful they are, so they will be what I need. So, if I have this string: Hello **bold** world How…
Entity
  • 7,972
  • 21
  • 79
  • 122
5
votes
4 answers

Complex non-greedy matching with regular expressions

I'm trying to parse rows from a HTML table with cells containing specific values with regular expressions in Python. My aim in this (contrived) example is to get the rows with "cow". import re response = '''
5
votes
3 answers

Why does a simple .*? non-greedy regex greedily include additional characters before a match?

I have a very simple regex similar to this: HOHO.*?_HO_ With this test string... fiwgu_HOHO_HOHO_HOHOrgh_HOHO_feh_HOHO___HO_fbguyev I expect it to match just _HOHO___HO_ (shortest match, non-greedy) Instead it matches…
user56reinstatemonica8
  • 32,576
  • 21
  • 101
  • 125
4
votes
1 answer

How to find a sentence containing a phrase in text using python re?

I have some text which is sentences, some of which are questions. I'm trying to create a regular expression which will extract only the questions which contain a specific phrase, namely 'NSF' : import re s = "This is a string. Is this a question?…
Erik
  • 132
  • 1
  • 7
1
2
3
12 13