Questions tagged [regex-greedy]

The greedy regex property causes the regex engine to repeat a regex token as often as possible. Only if that causes the entire regex to fail, give up the last iteration, and proceed with the remainder of the regex. The greedy regex tokens are `+`, `*`, `?` and the repetition using curly braces.

Example of Greediness

Using a regex to match an HTML tag the regular expression does not need to exclude any invalid use of sharp brackets. An HTML tag will be anything between sharp brackets.

If the test string is the following:

This is a <EM>first</EM> test.

With the <.+> patterh a expected match would be <EM> and when continuing after </EM>. But he regex will match <EM>first</EM>.

The reason is that the plus is a greedy token.

969 questions
5
votes
5 answers

Why does my non-greedy Perl regex match nothing?

I thought I understood Perl RE to a reasonable extent, but this is puzzling me: #!/usr/bin/perl use strict; use warnings; my $test = "'some random string'"; if($test =~ /\'?(.*?)\'?/) { print "Captured $1\n"; print "Matched…
Sundar R
  • 13,776
  • 6
  • 49
  • 76
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
5 answers

Regular expressions in swift

I'm bit confused by NSRegularExpression in swift, can any one help me? task:1 given ("name","john","name of john") then I should get ["name","john","name of john"]. Here I should avoid the brackets. task:2 given ("name"," john","name of…
Damodar
  • 707
  • 2
  • 10
  • 23
5
votes
1 answer

RegEx for matching dates (Month Day, Year OR m/d/yy)

I'm trying to write a regex expression that can be used to find dates in a string that may be preceded (or followed) by spaces, numbers, text, end-of-line, etc. The expression should handle US date formats that are either 1) Month Name Day, Year -…
TedS
  • 53
  • 1
  • 1
  • 5
5
votes
1 answer

Replace line breaks except inside
 tags with brackets(<>) inside 
 tags

I replaced all the line breaks outside pre tags using the answer available in the question. \n(?![^<]*<\/pre>) It was working fine until the content in pre tag had < or > brackets. For example, with input of:

Test contennt for regex with line…

hari prasath
  • 324
  • 2
  • 9
5
votes
3 answers

Need regexp to find substring between two tokens

I suspect this has already been answered somewhere, but I can't find it, so... I need to extract a string from between two tokens in a larger string, in which the second token will probably appear again meaning... (pseudo code...) myString =…
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
5
votes
1 answer

How to assign text with in braces into variable in vim

Is there any way to get data with in braces, before a certain latex command (\body) and assign that content (long text) to a variable. eg: \text{just a text before body} \body{contains lot of paragraphs etc etc etc, and that paragraphs also…
Zam
  • 367
  • 2
  • 17
5
votes
4 answers

match a regular expression with optional lookahead

I have the following strings: NAME John Nash FROM California NAME John Nash I want a regular expression capable of extracting 'John Nash' for both strings. Here is what I tried "NAME(.*)(?:FROM)" "NAME(.*)(?:FROM)?" "NAME(.*?)(?:FROM)?" but none of…
Dayvid Oliveira
  • 1,157
  • 2
  • 14
  • 34
5
votes
1 answer

How to match nth occurrence in a string using regular expression

How to match nth occurrence in a string using regular expression set test {stackoverflowa is a best solution finding site stackoverflowb is a best solution finding site stackoverflowc is a best solution finding sitestackoverflowd is a best…
velpandian
  • 431
  • 4
  • 11
  • 23
5
votes
3 answers

Why is the star quantifier greedier than the plus quantifier in Java regular expressions?

I have text I'm trying to extract from LogicalID and SupplyChain from SupplyChain At first I used the following regex: .*([A-Za-z]+)>([A-Za-z]+)<.* This matched as follows: ["D", "SupplyChain"] In a fit of desperation, I…
duber
  • 2,769
  • 4
  • 24
  • 32
4
votes
3 answers

Strange behavior in regexes

There was a question about regex and trying to answer I found another strange things. String x = "X"; System.out.println(x.replaceAll("X*", "Y")); This prints YY. why?? String x = "X"; System.out.println(x.replaceAll("X*?", "Y")); And this…
shift66
  • 11,760
  • 13
  • 50
  • 83
4
votes
2 answers

Regex for repeating series of numbers and number ranges (e.g. 3 digit numbers and 3 digit number ranges)

I'm looking for a regex to match repeating number sequences. The number/range itself could be any three digit number, for e.g. I want to match 345 346-348 234,235,236,237-239 234, 235, 236, 237-239 234,234, 236 and 237-239 234,234, 236 or…
Bob76
  • 477
  • 1
  • 5
  • 12
4
votes
1 answer

Positive Lookbehind greedy

I think I have some misunderstanding about how a positive Lookbehind works in Regex, here is an example: 12,2 g this is fully random 89 g random string 2 0,6 oz random stuff 1 really random stuff Let's say I want to match everything after the…
mnd
  • 63
  • 5
4
votes
1 answer

Greedy behaviour of grep

I thought that in regular expressions, the "greediness" applies to quantifiers rather than matches as a whole. However, I observe that grep -E --color=auto 'a+(ab)?' <(printf "aab") returns aab rather than aab. The same applies to sed. On the…
Joseph Stack
  • 566
  • 5
  • 14
4
votes
4 answers

RegEx for matching a word after specific word in multiple lines

There is regex feature to find words instead of "Ctrl + F" in some editor like VS Code, I'm trying to find a word after a specific word with some another lines. For example, how to use regex to filter those "someFunction" with the specific…