473,395 Members | 1,676 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,395 software developers and data experts.

Regex: Not capturing '+' character

I have this pattern: @"\s*//\$\$\s*Targets:\s*([\s,\w]+)"

to match this line: //$$Targets: thing, thing+

When I get a match, the first Group (index 1, not 0) has: "thing, thing" -
it drops the plus ('+'). Now, I don't know the plus will be there,
everything after "Targets:" is variable and will change so I can't simply
add an explicit check for it.

What trick is there to get the special characters returned in the groups?

Thanks for reading,
Steve
Apr 16 '06 #1
3 1402
sklett <sk****@mddirect.com> wrote:
I have this pattern: @"\s*//\$\$\s*Targets:\s*([\s,\w]+)"

to match this line: //$$Targets: thing, thing+

When I get a match, the first Group (index 1, not 0) has: "thing, thing" -
it drops the plus ('+'). Now, I don't know the plus will be there,
everything after "Targets:" is variable and will change so I can't simply
add an explicit check for it.

What trick is there to get the special characters returned in the groups?


Well, you've currently said you want to match whitespace characters,
commas, and word characters. Any extra characters you want to capture
need to be in the [...] part. So, to get + as well, you'd need:

[\s,\w\+]

To you just want to match to the end of the line though? If so, just
use (.+) instead of ([\s,\w]+).

(You might want to use the $ "end-of-string" assertion, depending on
what other options you have, such as multi-line.)

--
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
Apr 16 '06 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
sklett <sk****@mddirect.com> wrote:
I have this pattern: @"\s*//\$\$\s*Targets:\s*([\s,\w]+)"

to match this line: //$$Targets: thing, thing+

When I get a match, the first Group (index 1, not 0) has: "thing,
thing" -
it drops the plus ('+'). Now, I don't know the plus will be there,
everything after "Targets:" is variable and will change so I can't simply
add an explicit check for it.

What trick is there to get the special characters returned in the groups?
Well, you've currently said you want to match whitespace characters,
commas, and word characters. Any extra characters you want to capture
need to be in the [...] part. So, to get + as well, you'd need:

[\s,\w\+]

To you just want to match to the end of the line though? If so, just
use (.+) instead of ([\s,\w]+).

(You might want to use the $ "end-of-string" assertion, depending on
what other options you have, such as multi-line.)


Jon, perfect, thank you! Regex is sooo much faster than string comparison
parsing, I can't believe it.

--
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

Apr 18 '06 #3
sklett <sk****@mddirect.com> wrote:
Well, you've currently said you want to match whitespace characters,
commas, and word characters. Any extra characters you want to capture
need to be in the [...] part. So, to get + as well, you'd need:

[\s,\w\+]

To you just want to match to the end of the line though? If so, just
use (.+) instead of ([\s,\w]+).

(You might want to use the $ "end-of-string" assertion, depending on
what other options you have, such as multi-line.)


Jon, perfect, thank you! Regex is sooo much faster than string comparison
parsing, I can't believe it.


Well, there are times that it's faster, and there are plenty of times
that it's slower. For instance, if a matching string is always just
//$$Targets: (stuff)
and you want to get (stuff) - all the way to the end of the string -
then using IndexOf and Substring would be simpler and almost certainly
faster.

--
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
Apr 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BMermuys | last post by:
Hi, string test="dit is een test"; bool bMatch = Regex.IsMatch( test, "^(|)*$" ); after executing, the bMatch is false The pattern is not optimized and seems a little stupid. But for large...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
6
by: Lubomir | last post by:
Hi, I am using the following pattern: "\\b" + MySttring + "\\b" If MyString is "one", this should pick up whole words like "one". The problem is, it will pick up also the word: "one.two"...
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 ...
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.