By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,106 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,106 IT Pros & Developers. It's quick & easy.

How to make a c# regex match only the *last* occurrence of a pattern?

P: n/a
Hi,

I've been searching as best I can for this - coming up with little.

I have a file that is full of lines fitting this pattern:

(?<year>\d{4}),(?<amount>\d{6,7})

I'm likely to get a bunch of hits with this - I'm only interested in
the *last* one. Is there a way to build the concept "last" into the
regex itself? Or is taking the last element of the match array the only
thing to do?

I would just prefer to avoid have a potentially long array being sent
around...

Thanks,

cdj

Nov 8 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
One suggestion is to find the last index of the newline character, and regex
everything after that, if now match, last index it again on everything before
it and they the reg ex on that line. you could keep doing this till a match
occurs. I'm not a regex expert but I dont believe you can specify the last
match.

HTH

Ciaran

"sherifffruitfly" wrote:
Hi,

I've been searching as best I can for this - coming up with little.

I have a file that is full of lines fitting this pattern:

(?<year>\d{4}),(?<amount>\d{6,7})

I'm likely to get a bunch of hits with this - I'm only interested in
the *last* one. Is there a way to build the concept "last" into the
regex itself? Or is taking the last element of the match array the only
thing to do?

I would just prefer to avoid have a potentially long array being sent
around...

Thanks,

cdj

Nov 9 '06 #2

P: n/a
Hi,

You can find what you want by searching for the occurrence of your
pattern that does not have _another_ occurrence of that pattern after
it.

Suppose your pattern is P. You can search for the last P by doing a
search for "anything" followed by "P" followed by "anything but P".

Or you can try google (d'oh, right? :D "pattern matching c# last"):

http://www.google.nl/search?hs=ue5&h...G=Zoeken&meta=

In short, the solution presented at the first hit from google is to use
the RegexOptions.RightToLeft parameter :P

-Jeroen

Nov 9 '06 #3

P: n/a
Like I said, I'm no regex guru so I offered a coding way round it. The regex
option is undoubtedly better.

Ciaran O'Donnell

"Jeroen" wrote:
Hi,

You can find what you want by searching for the occurrence of your
pattern that does not have _another_ occurrence of that pattern after
it.

Suppose your pattern is P. You can search for the last P by doing a
search for "anything" followed by "P" followed by "anything but P".

Or you can try google (d'oh, right? :D "pattern matching c# last"):

http://www.google.nl/search?hs=ue5&h...G=Zoeken&meta=

In short, the solution presented at the first hit from google is to use
the RegexOptions.RightToLeft parameter :P

-Jeroen

Nov 9 '06 #4

P: n/a

Jeroen wrote:
Hi,

You can find what you want by searching for the occurrence of your
pattern that does not have _another_ occurrence of that pattern after
it.

Suppose your pattern is P. You can search for the last P by doing a
search for "anything" followed by "P" followed by "anything but P".

Or you can try google (d'oh, right? :D "pattern matching c# last"):
Which I did long before I posted the question. I saw the right-to-left
trick. I was simply hoping for a "pure regex solution", rather than an
implementation-specific solution.

I'll see if I can get your first idea to work for me though - thanks a
bunch!

cdj

Nov 9 '06 #5

P: n/a
In article <11**********************@k70g2000cwa.googlegroups .com>,
sherifffruitfly <sh*************@gmail.comwrote:

: I have a file that is full of lines fitting this pattern:
:
: (?<year>\d{4}),(?<amount>\d{6,7})
:
: I'm likely to get a bunch of hits with this - I'm only interested in
: the *last* one. Is there a way to build the concept "last" into the
: regex itself? Or is taking the last element of the match array the only
: thing to do?
:
: I would just prefer to avoid have a potentially long array being sent
: around...

Start your pattern with .* to grab the last match. For example:

using System;
using System.Text.RegularExpressions;

namespace consapp
{
class Program
{
static void Main(string[] args)
{
const string pattern = @"(?<year>\d{4}),(?<amount>\d{6,7})";
Regex regex;
Match m;
string input = "1234,1234569999,123456";

Console.WriteLine("Vanilla pattern:");
regex = new Regex(pattern);
m = regex.Match(input);
Console.WriteLine(" - year=[{0}], amount={1}",
m.Groups["year"], m.Groups["amount"]);

Console.WriteLine("Leading dot-star:");
regex = new Regex(".*" + pattern);
m = regex.Match(input);
Console.WriteLine(" - year=[{0}], amount={1}",
m.Groups["year"], m.Groups["amount"]);
}
}
}

Output:

Vanilla pattern:
- year=[1234], amount=1234569
Leading dot-star:
- year=[9999], amount=123456

Hope this helps,
Greg
--
Criminal means, once tolerated, are soon preferred.
-- Edmund Burke
Nov 20 '06 #6

P: n/a

Greg Bacon wrote:
>
Start your pattern with .* to grab the last match. For example:
Thank you!

Nov 21 '06 #7

P: n/a
sherifffruitfly wrote:
Which I did long before I posted the question. I saw the right-to-left
trick. I was simply hoping for a "pure regex solution", rather than an
implementation-specific solution.

I'll see if I can get your first idea to work for me though - thanks a
bunch!
Well, it might have been a good idea to mention this - something like
"I know about the right-to-left flag, but I want a portable solution."

Fwiw, while using .* to skip to the end and work backward should be
pretty fast, it will only let you find the last match. Using the
right-to-left flag will let you quickly find the last N matches.

--

www.midnightbeach.com/.net
What you need to know.
Nov 21 '06 #8

P: n/a

Jon Shemitz wrote:
sherifffruitfly wrote:
Which I did long before I posted the question. I saw the right-to-left
trick. I was simply hoping for a "pure regex solution", rather than an
implementation-specific solution.
Well, it might have been a good idea to mention this - something like
"I know about the right-to-left flag, but I want a portable solution."
Sigh.

You DON'T google before asking, and you get reamed.

You DO google before asking, and still get reamed.
Thanks.

Nov 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.