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

Regular Expression help

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.
Jul 21 '05 #1
9 1417
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
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.


This seems to work fine:
([^' ]+)|'([^']+)'

Unfortunately, you'll have to remove the 's manually from the matches, or
query both groupings.

Niki
Jul 21 '05 #2
"Niki Estner" <ni*********@cube.net> wrote in message news:<ee*************@TK2MSFTNGP11.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
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.


This seems to work fine:
([^' ]+)|'([^']+)'

Unfortunately, you'll have to remove the 's manually from the matches, or
query both groupings.

Niki


How do I use this with the Split method in the Regex class?

Mark
Jul 21 '05 #3
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
"Niki Estner" <ni*********@cube.net> wrote in message
news:<ee*************@TK2MSFTNGP11.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
>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.


This seems to work fine:
([^' ]+)|'([^']+)'

Unfortunately, you'll have to remove the 's manually from the matches, or
query both groupings.

Niki


How do I use this with the Split method in the Regex class?


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
Jul 21 '05 #4
"Niki Estner" <ni*********@cube.net> wrote in message news:<uR**************@TK2MSFTNGP14.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
"Niki Estner" <ni*********@cube.net> wrote in message
news:<ee*************@TK2MSFTNGP11.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
>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.

This seems to work fine:
([^' ]+)|'([^']+)'

Unfortunately, you'll have to remove the 's manually from the matches, or
query both groupings.

Niki


How do I use this with the Split method in the Regex class?


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


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
Jul 21 '05 #5
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6*************************@posting.google.co m...
"Niki Estner" <ni*********@cube.net> wrote in message
news:<uR**************@TK2MSFTNGP14.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
> "Niki Estner" <ni*********@cube.net> wrote in message
> news:<ee*************@TK2MSFTNGP11.phx.gbl>...
>> "Mark Downes" <ma*********@bradycorp.com> wrote in
>> news:c6**************************@posting.google.c om...
>> >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.
>>
>> This seems to work fine:
>> ([^' ]+)|'([^']+)'
>>
>> Unfortunately, you'll have to remove the 's manually from the matches,
>> or
>> query both groupings.
>>
>> Niki
>
> How do I use this with the Split method in the Regex class?


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


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.


If this doesn't work, please post some sample data that demonstrates your
problem:
([^' ]+)|'([^']*)'

Niki
Jul 21 '05 #6
"Niki Estner" <ni*********@cube.net> wrote in message news:<#E**************@TK2MSFTNGP10.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6*************************@posting.google.co m...
"Niki Estner" <ni*********@cube.net> wrote in message
news:<uR**************@TK2MSFTNGP14.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
> "Niki Estner" <ni*********@cube.net> wrote in message
> news:<ee*************@TK2MSFTNGP11.phx.gbl>...
>> "Mark Downes" <ma*********@bradycorp.com> wrote in
>> news:c6**************************@posting.google.c om...
>> >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.
>>
>> This seems to work fine:
>> ([^' ]+)|'([^']+)'
>>
>> Unfortunately, you'll have to remove the 's manually from the matches,
>> or
>> query both groupings.
>>
>> Niki
>
> How do I use this with the Split method in the Regex class?

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


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.


If this doesn't work, please post some sample data that demonstrates your
problem:
([^' ]+)|'([^']*)'

Niki


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
Jul 21 '05 #7
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
....
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.


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
Jul 21 '05 #8
"Niki Estner" <ni*********@cube.net> wrote in message news:<e1**************@TK2MSFTNGP10.phx.gbl>...
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
....
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.


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


I found a solution to the problem in the article 'Managed Extensions:
Parsing CSV Files with Regular Expressions' at
http://www.codeguru.com/Cpp/Cpp/stri...icle.php/c8153

I used it with the Regex.Split method and it worked perfectly. The
regular expression is ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))".

Thanks for all of your help Niki!

Mark
Jul 21 '05 #9
"Mark Downes" <ma*********@bradycorp.com> wrote in
news:c6**************************@posting.google.c om...
...
I found a solution to the problem in the article 'Managed Extensions:
Parsing CSV Files with Regular Expressions' at
http://www.codeguru.com/Cpp/Cpp/stri...icle.php/c8153

I used it with the Regex.Split method and it worked perfectly. The
regular expression is ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))".


Note that:
a) this will have to scan the rest of the string after each comma, so it has
O(n²) performance - bad for long strings of data, and
b) it will only work correctly on a well-formed input string

If your input strings are short, and always well-formed (i.e. even number or
"'s), it should work fine!

Niki
Jul 21 '05 #10

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

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
18
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.