473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex to extract after final hyphen

How to write a Regex that extracts whatever follows the final hyphen in a
string ?
Mar 29 '06 #1
3 1641
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
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
<"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: G. Delavaquerie | last post by:
I'm becoming nuts with regex and preg_match_all. I hope someone could help ... Here is my problem : Match</td> <td align="center" valign="middle" class="textnoir">final</td> <td align="center"...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
5
by: skavan | last post by:
Hi, I'm just wrapping my head around regex and am pretty sure it can do the task at hand - but it's too complex for my brain to process -- so am throwing it out there for you experts to comment...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
3
by: Praveen | last post by:
looking for regex pattern for validating emailid emailid can have a-z 0-9 - _ . (a to z, 0 ot 9, hyphen,undercore, dot) here is a sample which I got from net which doesnt allow hyphen(-) ...
2
by: Smokey Grindel | last post by:
I basically need to remove any leading zeros and hyphens from a string... regex seemed like the best rout and using a replace regex method... anyone know of any good strings for this? The one I...
31
by: raylopez99 | last post by:
I went through a bunch of Regex examples, and indeed it's quite powerful, including 'groups' using 'matches', word boundaries, lookahead matches, replacing and splitting text,etc--apparently...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.