Search:
Namespaces
Discussions
.NET v1.1
Feedback
Regular Expression help
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.general
.
Post a new message to this list...
Mark Downes
I need to split a string based on a character, such as a space (0x20).
The caveat is that I need to ignore the character if it is found
between a pair of some other characters, such as single-quotes. For
example (where _ denotes a space character):
ab_cd_'ef'_'g_h'_i_'j_k_l'
I should get back
ab
cd
ef
g_h
i
j_k_l
I'm sure there is an easy solution to this if you are a regex guru.
As for me, I'm clueless.
Reply to this message...
Niki Estner
"Mark Downes" <
Click here to reveal e-mail address
> wrote in
news:
Click here to reveal e-mail address
...
[Original message clipped]
This seems to work fine:
([^' ]+)|'([^']+)'
Unfortunately, you'll have to remove the 's manually from the matches, or
query both groupings.
Niki
Reply to this message...
Mark Downes
"Niki Estner" <
Click here to reveal e-mail address
> wrote in message news:<
Click here to reveal e-mail address
>...
[Original message clipped]
How do I use this with the Split method in the
Regex
class?
Mark
Reply to this message...
Niki Estner
"Mark Downes" <
Click here to reveal e-mail address
> wrote in
news:
Click here to reveal e-mail address
...
[Original message clipped]
I don't think you can do what you want with the split method, at least not
in a performant way; You'll have to use
Regex
.Matches to get a list of all
the matches.
Niki
Reply to this message...
Mark Downes
"Niki Estner" <
Click here to reveal e-mail address
> wrote in message news:<
Click here to reveal e-mail address
>...
[Original message clipped]
Thanks Niki for your help. I'm really close to what I need. The only
thing that I'm missing is that I need the regex expression to work
like the
String
.Split method where two adjacent delimiters gives back
an empty string and a delimiter at the beginning or end of a string
gives back an empty string.
I'm using the
Regex
.Matches method like you suggested and I get back
the correct data whenever the aforementioned cases are absent from the
string.
Do you have any advice?
Mark
Reply to this message...
Niki Estner
"Mark Downes" <
Click here to reveal e-mail address
> wrote in
news:
Click here to reveal e-mail address
...
[Original message clipped]
If this doesn't work, please post some sample data that demonstrates your
problem:
([^' ]+)|'([^']*)'
Niki
Reply to this message...
Mark Downes
"Niki Estner" <
Click here to reveal e-mail address
> wrote in message news:<#Enw$
Click here to reveal e-mail address
>...
[Original message clipped]
Here is an example set of data where I'm trying to split the data from
a comma-delimited string with double-quotes around strings (all one
line, watch out for wraps):
"50-00-0","Formalin",3,4,0,,"DANGER","Corrosive, Flammable","Eyes,
Skin, Respiratory System, Kidney","Goggles, Fshield, Gloves, Fullsuit,
Boots, ChkResp"
I changed the expression to: ([^\",]+)|\"([^\"]*)\" in CSharp.
For the data, I need to get back the following split data:
"50-00-0"
"Formalin"
3
4
0
<empty string>
"DANGER"
"Corrosive, Flammable"
"Eyes, Skin, Respiratory System, Kidney"
"Goggles, Fshield, Gloves, Fullsuit, Boots, ChkResp"
With the expression I get back all of the correct data, but I'm
missing the empty string where there was no value listed in the data.
Mark
Reply to this message...
Niki Estner
"Mark Downes" <
Click here to reveal e-mail address
> wrote in
news:
Click here to reveal e-mail address
...
[Original message clipped]
The regex engine doesn't like to return empty matches. You can however
include the comma in the match like this:
\G(([^\",]*)|\"([^\"]*)\")\s*(,|$)
And use only the first capture group (to remove the comma).
Does this work?
Niki
Reply to this message...
System.String
System.Text.RegularExpressions.Regex
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com