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

Regex get all matches including smaller submatches

I have following input string Testing bold italic text. and following regex : <([A-Z][A-Z0-9]*)\b[^>]*>.* This regex only gives following larger match bold italic How to use regex to get the smaller match…
0
votes
3 answers

How to match something like shortcode that repeats and contains whatever including new lines

I am trying to process something similar like shrotcode via preg_replace_callback function: $new_content = preg_replace_callback("|\[BLOCK\s?(TYPE=[0-9a-z\/]+)?\s?(TEXT=[a-z]+)?\s?(LAST)?\s?\]((?:(?!BLOCK).)*)\[\/BLOCK\]|","block",$content); The…
0
votes
1 answer

Xcode lazy regular expression

I'm trying to replace something like this: NSSomeFunction(@"some var", @"another one") With: NSSomeOhterFunction(@"some var") In Xcode. So these are source files... I bet the regular expression will look something like this: NSSomeFunction\((.*),…
quano
  • 18,812
  • 25
  • 97
  • 108
0
votes
1 answer

regex matching in python not working properly - Overlapping matches?

I am experimenting with for regex lazy matches. when I tried this: >>> text='Hello <>World' >>> pattern3=re.compile('<.*?>') >>> for mat in re.findall(pattern3,text): ... print mat ... <> It works as expected, and gives the…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
0
votes
1 answer

Regex for extracting qmake variables

I'm trying to write the QRegExp for extracting variable names from qmake project code (*.pro files). The syntax of variable usage have two forms: $$VAR $${VAR} So, my regular expression must handle both cases. I'm trying to write expression in…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
0
votes
1 answer

RegEx HTML matching too much with lazy wildcard

RegEx: TheTextToFind HTML: DON'T_WANT_THIS_MATCHED TheTextToFind Why does the match include this?
bradvido
  • 2,743
  • 7
  • 32
  • 49
0
votes
2 answers

why regular expression .*? only match an empty string in javascript?

/.*?/.exec("abc");//output [""] I think .*? is non-greedy and it should return a
looping
  • 1,141
  • 3
  • 11
  • 20
0
votes
3 answers

Python regex for select/extract from nested groups

I am trying to process a string with CHAR(int) and NCHAR(int) to convert those instances with their ASCII counter-parts. An example would be something like this: CHAR(124) + (SELECT TOP 1 CAST(name AS VARCHAR(8000)) FROM (SELECT TOP 1 colid, name…
Web User
  • 7,438
  • 14
  • 64
  • 92
0
votes
2 answers

Tcl/Expect regular expression - want to make lazy (as opposed to greedy)

New to the site, so bear with me. I'm working on a Tcl/Expect script and trying to match part of the 4th line in the following router output (two possible outputs shown). It will usually have an IP address, but may have a string like in the second…
James
  • 1,186
  • 9
  • 14
0
votes
1 answer

Regex: match SQL PRINT blocks with quoted text in it

I have the following text I am trying match using regular expressions: PRINT CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 111) + ' ' + CONVERT(NVARCHAR, CURRENT_TIMESTAMP, 108) + ' -Test Mode : ' + (CASE WHEN @turbo_mode_ind = 1…
JonasVH
  • 1
  • 1
0
votes
1 answer

How can I properly handle greedy and optional patterns in python regex

I have a variable which contains a path and I want to extract the 6th folder from the path, but the 7 folder may or may not occur. In both cases... the regex should return "three", but the first example fails to match. I tried using ? to indicate…
panofish
  • 7,578
  • 13
  • 55
  • 96
0
votes
2 answers

How do I use the non-greedy modifier when searching backwards?

I have some queries like: "$Prefex" SELECT column_name(s) FROM table_name WHERE pal pal pal LIMIT "$Suffix" These queries have unwanted prefixes and suffixes. I used a regex to get rid of the prefix using a non-greedy .*?SELECT. I couldn't find a…
EurikaIam
  • 136
  • 9
0
votes
2 answers

Returning only the first match using Regex Look-Behinds

Given the following XML document: How can I return the first /> using Regex? So far I've been able to get pretty…
mclark1129
  • 7,532
  • 5
  • 48
  • 84
0
votes
4 answers

Java Regexp: UNGREEDY flag

I'd like to port a generic text processing tool, Texy!, from PHP to Java. This tool does ungreedy matching everywhere, using preg_match_all("/.../U"). So I am looking for a library, which has some UNGREEDY flag. I know I could use the .*? syntax,…
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
0
votes
3 answers

Most non-greedy regex match in python (or just simply regex in general

Im having an issue where my regex is matching too much. I've tried making it as non-greedy as possible. My RE is: define host( |\t)*{(.*\n)*?( |\t)*host_name( |\t)*HOST_B(.*\n)*?( |\t)*} meaning "define host" followed by any spaces or tabs…
user2072710
  • 77
  • 1
  • 7