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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification