greedy

How can I fix my regex to not match too much with a greedy quantifier?

Hi everyone! I have the following line: "14:48 say;0ed673079715c343281355c2a1fde843;2;laka;hello ;)" I parse this by using a simple regexp: if($line =~ /(\d+:\d+)\ssay;(.*);(.*);(.*);(.*)/) { my($ts, $hash, $pid, $handle, $quote) = ($1, $2, $3, $4, $5); } But the ; at the end messes things up and I don't know why. Shouldn't th...

How do I parse this correctly with spirit?

My situation: I'm new to Spirit, I have to use VC6 and am thus using Spirit 1.6.4. I have a line that looks like this: //The Description;DESCRIPTION;; I want to put the text DESCRIPTION in a string if the line starts with //The Description;. I have something that works but looks not that elegant to me: vector<char> vDescription; //...

Why does my Boost.Regex search report only one match iteration?

I am trying to find out how many regex matches are in a string. I'm using an iterator to iterate the matches, and and integer to record how many there were. long int before = GetTickCount(); string text; boost::regex re("^(\\d{5})\\s(\\d{8})\\s(.*)\\s(.*)\\s(.*)\\s(\\d{8})\\s(.{1})$"); char * buffer; long length; long count; ifstream ...

Why does my non-greedy Perl regex match nothing?

Hi, 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 $&"; } else { print "What?!!"; } prints Captured Matched ' It seems it has ...

Regex: Need help with greedy quantifier

I'm doing a simple search-and-replace in Perl, but I need some help. These are lines in a file: 1001(seperator could be "anything")john-1001(seperator could be "anything")mark 1001(seperator could be "anything")mark-1001(seperator could be "anything")john I wanna assign a new userID for john, like 2001. So this is the result I want:...

Non greedy regex matching in sed?

I'm trying to use sed to clean up lines of URLs to extract just the domain.. e.g., from: http://www.suepearson.co.uk/product/174/71/3816/ I want: http://www.suepearson.co.uk/ (either with or without the trainling slash, it doesn't matter) I have tried: sed 's|\(http:\/\/.*?\/\).*|\1|' and (escaping the non greedy quantifier) ...

Ignoring an optional suffix with a greedy regex

I'm performing regex matching in .NET against strings that look like this: 1;#Lists/General Discussion/Waffles Win 2;#Lists/General Discussion/Waffles Win/2_.000 3;#Lists/General Discussion/Waffles Win/3_.000 I need to match the URL portion without the numbers at the end, so that I get this: Lists/General Discussion/Waffles Win T...

How can you be sure that a problem exhibits "Greedy choice property"?

I am afraid that there might be a situation for which the "greedy choice property" might not hold. For any problem, I can only check for small data-sets. What if, for large data-sets, the property fails? Can we ever be sure? ...

php regex: lookbehind and lookahead and greediness problem

This should be simple but I'm a noob and I can't for the life of me figure it out. I'm trying to use regex to match text inside of special open/close tags: [p2][/p2] So in this text: apple [p2]banana[/p2] grape [p2]lemon[/p2] it should match "banana" and "lemon". The regex I've worked up so far is: (?<=\[p2\]).+(?=\[\/p2\]) But ...

What is the difference between Greedy-Search and Uniform-Cost-Search?

When searching in a tree, my understanding of uniform cost search is that for a given node A, having child nodes B,C,D with associated costs of (10, 5, 7), my algorithm will choose C, as it has a lower cost. After expanding C, I see nodes E, F, G with costs of (40, 50, 60). It will choose 40, as it has the minimum value from both 3. Now...

Regex is behaving lazy, should be greedy

I thought that by default my Regex would exhibit the greedy behavior that I want, but it is not in the following code: Regex keywords = new Regex(@"in|int|into|internal|interface"); var targets = keywords.ToString().Split('|'); foreach (string t in targets) { Match match = keywords.Match(t); Console.WriteLine("Matched {0,...

How to create a maze (labyrinth) in NetLogo?

I am new to NetLogo software and I am trying to create a 5x5 grid with 2 exits and put some walls in it (in other words i want to create a maze or a labyrinth). I was wondering if there is a way to make thicker or change the colour of only the side and not the whole patch. I want to put only one agent inside and let him find the exit b...

javascript match isn't greedy

given text:1,0397,030 followed by nothing or any number of characters not in the set [,0-9] why is my regex not being greedy enough? Javascript's regex is supposed to be greedy by default, right? but text.match('[,0-9]+'); pulls back 1,0397 instead of 1,0397,030 var matches = shortInput.match('[0-9]+[,0-9]*[0-9]*'); works. This is in...

Point covering problem

I recently had this problem on a test: given a set of points m (all on the x-axis) and a set n of lines with endpoints [l, r] (again on the x-axis), find the minimum subset of n such that all points are covered by a line. Prove that your solution always finds the minimum subset. The algorithm I wrote for it was something to the effect ...

In regex is it called lazy or non-greedy?

I originally heard it as nongreedy. Then on references 'on the web' i saw it called as lazy. Which is it? ...

Find a tag with a specific suffix using preg_match

I'm looking for tags that end in a specific way using regular expressions in PHP. However all my attempts either result in too much or too little. For example, in the following string I'd like to match 'bar' because it is in a tag that ends with 'suffix'. preg_match_all("/<(.*?)suffix>/", "<foo> <barsuffix> <baz>" However the above l...

Greedy versus Non-Greedy matching in Python re

Please help me to discover whether this is a bug in Python (2.6.5), in my competence at writing regexes, or in my understanding of pattern matching. (I accept that a possible answer is "Upgrade your Python".) I'm trying to parse a Yubikey token, allowing for the optional extras. When I use this regex to match a token without any opti...

An ungreedy regular expression to fetch title from a page

Hello I want to get the title of a web page using C#. So i'm requesting that page and getting the response page and using a regular expression to extract the title. string regex = @"(?<=)([\s\S]*)(?=)/"; But i saw a site having two titles such as <title>Page Title</title> <link href="...."/> <title>Page Title</title> So when i use ...

Regular expression troubles, can't seem to match what I want

I have a load of jibberish data with this somewhere in the middle: "video_id": "hGosI8rBVe8" And from this, I want to extract hGosI8rBVe8. Note that what I want to extract can be of any length, and can include upper/lowercase letters and numbers. This is what I've tried so far: "video_id": "(.*)" and: "video_id": "([a-zA-Z0-9]*)" ...

Greedy, Non-Greedy, All-Greedy Matching in C# Regex

Hi, How can I get all the matches in the following example: // Only "abcd" is matched MatchCollection greedyMatches = Regex.Matches("abcd", @"ab.*"); // Only "ab" is matched MatchCollection lazyMatches = Regex.Matches("abcd", @"ab.*?"); // How can I get all matches: "ab", "abc", "abcd" Thanks. Peter P.S.: I want to have the all...