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

Using regex to eliminate chunks in a file (categorized events in iCal file)

I have one .ics file from which I would like to create individual new .ics files depending on the event categories (I can't get egroupware to export only events of one category, I want to create new calendars depending on category). My intended…
PiEnthusiast
  • 314
  • 1
  • 4
  • 19
0
votes
2 answers

Extracting quoted and unquoted values using regex

I'm trying to to parse a string of type = using regular expressions but have hit some issues adding support for quoted values. The idea is that any unquoted values should be trimmed of leading / trailing white space so that [ Hello ]…
Component 10
  • 10,247
  • 7
  • 47
  • 64
0
votes
3 answers

Ungreedy regexp in Perl

I'm trying to capture a string that is like this: document.all._NameOfTag_ != null ; How can I capture the substring: document.all._NameOfTag_ and the tag name: _NameOfTag_ My attempt so far: if($_line_ =~ m/document\.all\.(.*?).*/) { } but…
user63898
  • 29,839
  • 85
  • 272
  • 514
0
votes
1 answer

why is this regular expression returning only one match?

Here is my input: xxx999xxx888xxx777xxx666yyy xxx222xxx333xxx444xxx555yyy This is the expression: xxx.*xxx(?(.(?!xxx.*xxx))*?)xxx.*yyy It's returning 444. I'd like it to return both 444 and 777, but I can't get anywhere with this. I…
Josh
  • 805
  • 10
  • 22
0
votes
2 answers

Perl regex subsitute last occurrence

I have this input: AB2.HYNN.KABCDSEG.L000.G0001V00 AB2.HYNN.GABCDSEG.L000.G0005V00 I would like to remove all which finish by GXXXXVXX in the string. When i use this code: $result =~ s/\.G.*V.*$//g; print "$result \n"; The result is :…
Patrick
  • 47
  • 8
-1
votes
1 answer

How to match only the titles between tags, without returning the tags themselves, using regular expressions?

I want to match the titles of h1 to h6 in an HTML file, without returning the h tags themselves, using regular expressions. Consider the following piece of an HTML file. I want to match "Welcome to my Homepage", "SQL", "RegEx", but not "This is not…
hengxin
  • 1,867
  • 2
  • 21
  • 42
-1
votes
1 answer

Why does the reluctant quantifier select the next atom regexp argument?

I understood that selects according normal quantify and returns the minimum value, but I don't understand why it's define the selection of next regexp atom to not be greedy. Does anyone know if there is a primitive expression that represents a…
-1
votes
5 answers

regex in sed removing only the first occurrence from every line

I have the following file I would like to clean up cat file.txt MNS:N+ GYPA*01 or GYPA*M MNS:M+ GYPA*02 or GYPA*N MNS:Mc GYPA*08 or GYP*Mc MNS:Vw GYPA*09 or GYPA*Vw MNS:Mg GYPA*11 or GYPA*Mg MNS:Vr GYPA*12 or GYPA*Vr My desired…
Shahin
  • 1,196
  • 1
  • 8
  • 15
-1
votes
3 answers

sed substitution dropping chunks of text

I wanted to use SED to find and replace a small string of text within a number of files. Specifically the substitution I want to perform is: sed -e '/35=R/s/|131=.*|/|131=$UNIQUE|/g' $f Which is running within a bash script where $f is the…
Phill
  • 1
  • 1
-1
votes
1 answer

What's the difference between `/[a-z]??/g` and `/[a-z]?/g`? Will they match a position?

How the following code differ and how they work? I am learning regular expressions in javascript. I am confused about the following code. I want to know how they work, and their differences. "abc".replace(/[a-z]??/g, "-") //…
-1
votes
1 answer

My regex is matching before expected

I understand that using regex to parse html is frowned upon, but this is the solution I want to try first. I am trying to match what a great sentence this is as well as any characters or spacing that comes in between those words in the following…
BlahMclean
  • 11
  • 6
-1
votes
1 answer

How to make '?' non gready

I have a CLASSPATH value C:\Windows\abc;C:\Windows\def;C:\Windows\ghi and need to get C:\Windows\def. Value of CLASSPATH can vary except def of middle path (or only one path \..\..\parent\def be present in CLASSPATH). Regex is ;?(.*def) but it is…
Naive
  • 492
  • 3
  • 8
  • 20
-1
votes
2 answers

regexp with perl, ungreedy not working as expected

Could somebody explain to me, why this is not working: # echo '"Hello,1" "Hello,2" "Hello,3"' | perl -pe 's/".+?,3"/1/' 1 or # echo '"Hello,1" "Hello,2" "Hello,3"' | perl -pe 's/".+?,2"/1/' 1 "Hello,3" My intention was to replace/find only…
Dan
  • 33
  • 3
-1
votes
1 answer

Bash : Find string and remove c-commentary signs around it (preferably inplace edit)

I am bothered with this for a long time already: I need to match a string in a text file and remove the C-commentary around it. The edit should be inplace or into a new file (I will then move mv-command to push it). /* might be text here; …
simonarne
  • 1
  • 1
-1
votes
2 answers

Keep the TRUE non-greedy match using Perl regex

From the following word "tacacatac", I want to match "cat". It seems like the regex c.*?t should give me this, but I guess it starts with the first occurrence of "c" and then from there finds the next "t", and thus, matches "cacat". Is there a…
risraelsen
  • 253
  • 1
  • 2
  • 5
1 2 3
12
13