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

Regex to extract after final hyphen

P: n/a
How to write a Regex that extracts whatever follows the final hyphen in a
string ?
Mar 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bob
Hi John,
All I can think of is :
-+[\w]+

Then iterate through your matches collection to the last match, strip off
the hyphen.
HTH
Bob
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
How to write a Regex that extracts whatever follows the final hyphen in a
string ?

Mar 29 '06 #2

P: n/a
John,

Hopefully this will help you out.
Sample Data:
Windows 98- Windows 2000 SP4- Windows CE- Windows Millennium Edition-
Windows Mobile for Pocket PC- Windows Mobile for Smartphone- Windows
Server 2003- Windows XP Media Center Edition- Windows XP Professional x64
Edition- Windows XP SP2- Windows XP Starter Edition
Expression:
-(?<Remainder>[^-]+)$
Returns:
Windows XP Starter Edition
Should always give you the last section in a group called Remainder

eg:

Regex rex = new RegularExpression(@"-(?<Remainder>[^-]+)$");
Match mtc = rex.Match("Windows 98- Windows 2000 SP4- Windows CE- Windows
Millennium Edition- Windows Mobile for Pocket PC- Windows Mobile for
Smartphone- Windows Server 2003- Windows XP Media Center Edition- Windows
XP Professional x64 Edition- Windows XP SP2- Windows XP Starter Edition");
if (mtc.Success)
{
Trace.Write(mtc.Groups["Remainder"].Value);
}

Enjoy.

On Wed, 29 Mar 2006 09:08:10 +0800, John A Grandy
<johnagrandy-at-yahoo-dot-com> wrote:
How to write a Regex that extracts whatever follows the final hyphen in a
string ?


Mar 29 '06 #3

P: n/a
<"John A Grandy" <johnagrandy-at-yahoo-dot-com>> wrote:
How to write a Regex that extracts whatever follows the final hyphen in a
string ?


Any reason you particularly want a regex rather than a straight call to
String.Substring?

string x = original.Substring (original.LastIndexOf ('-')+1);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.