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
-1
votes
1 answer

Regular Expression: how to match least characters (non-greedy) but no less than N

I want to have such a method leastMatch(String input, String regExp, int n) input is the input text, regExp is the regular expression and n is the minimal amount of characters the result has. The return value is a head of the input text which…
elmar
  • 41
  • 1
  • 6
-1
votes
1 answer

Non-Greedy Regex

I am trying to get the specific qualifier for each instance of part#1AMTB00186 from the html below. I need it to return 4cyl 2.3L - F23A1, Balance Shaft and 4cyl 2.3L - F23A1, CAM. I believe my regex is greedy, but I cannot figure out how to make it…
-1
votes
1 answer

Perl split regex non greedy fit with ">" as a separator

i'd like to split string with two or more ">", split function should brake string in first ">" and others put to second sting in list. i try $text = "tobash> hubba -> http://nonexists100101.net"; @op = split(/>{1}/, $text); but split still breaks…
Mr. NoNe
  • 53
  • 1
  • 8
-2
votes
2 answers

powershell non greedy replace upto the pattern

I have a file with multiple lines as shown in the example below, but I need the chars mentioned in the pattern to be present in the output, in other words non-greedy match. I need a way to remove the first part of the line and get the line which…
Priya
  • 1
  • 1
-2
votes
1 answer

Non-greedy search for beginning of string

I have the following links to be extracted: [{"file":"https:\/\/www.rapidvideo.com\/loadthumb.php?v=FFIMB47EWD","kind":"thumbnails"}], "sources": [ …
Echchama Nayak
  • 971
  • 3
  • 23
  • 44
-2
votes
1 answer

python Non greedy regular expression searching too many data

String: 'str1str2str2str4' I want to search and get only first "td" tag which contains text: "str2". so I tried two different non greedy expressions as below: >>> mystring =…
dagpag
  • 3
  • 2
-2
votes
3 answers

Javascript regular expressions modifier U

I have a string "{1} {2} {4} {abc} {abs}" and the regular expression /\{(.+)\}/g In PHP regular expressions i can use modifier "U" (U modifier: Ungreedy. The match becomes lazy by default). If i use /\{(.+)\}/gU my response looks like…
-8
votes
1 answer

Bash: uppercase text inside html tag with sed

echo -e '

abcd

\n

efgh

' | sed 's#

(.*?)<\h1>#

\U&

#g' The desired output is:

ABCD

efgh

Any ideas? Thanks.
Roger
  • 8,286
  • 17
  • 59
  • 77
1 2 3
12
13