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
5 answers

REGEX extracting specific part non greedy

I'm new to Python 2.7. Using regular expressions, I'm trying to extract from a text file just the emails from input lines. I am using the non-greedy method as the emails are repeated 2 times in the same line. Here is my code: import re f_hand =…
PIMg021
  • 83
  • 1
  • 8
0
votes
0 answers

Matches Shortest in Between with Match

I am using C# to parse XML, and this happens to me. This is not exactly what I am doing, but the same idea. Single line option is turned on. So if I have a string: Start xxx A xxx Pattern xxx End Start xxx B xxx Pattern xxx End Start xxx C xxx…
0
votes
0 answers

How to find non-greedy regular expression matching in JetBrains PhpStorm Search and Replace

I'm using PhpStorm and I'd like to find shortest string between {{ and }} by using Regex. An example is like this: {{$ux['friend']['profile']['firstName']}} {{$ux['friend']['profile']['familyName']}} I tried this: \$(.*)(\['.*'\])+ which finds…
Omid
  • 41
  • 5
0
votes
1 answer

Why is non-greedy Python Regex not non-greedy enough?

I've implemented non-greedy regex on a group of string URLs, where I'm trying to clean them up so that they end after the .com (.co.uk etc). Some of them continued with ' or " or < after the desired cutoff, and so I used x =…
DanielSon
  • 1,415
  • 4
  • 27
  • 40
0
votes
1 answer

Same pattern in grep and sed select different values

I want to create a pattern for sed, which will find out 'type="" For this I tried to use the pattern type=".*\?" echo 'aa type="none" stretchChildren="first"' | sed s/'type=".*\?"'/hello/ Above is the sed command which prints aa hello Which…
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
0
votes
2 answers

Java Regular Expression (greedy/nongreedy)

So I'm trying to separate the following two groups formatted as: FIRST - GrouP second.group.txt The first group can contain any character The second group is a dot(.) delimited string. I'm using the following regex to separate these…
user2492270
  • 2,215
  • 6
  • 40
  • 56
0
votes
3 answers