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. 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
"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
"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
"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
"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
"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
"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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |