Hi
I want to split a string into an array and treat quoted phrases as words.
I was splitting on a space which worked fine.
char[] delimiterChars = {' '};
string[] arrValues = searchText.Split(delimiterChars);
This would split a sentence ...
this is a test
into an array of size 4.
But I want to split and preserve the data in double qoutes.
i.e. it treats anything inside two quotes as a word
this is a "really really long" test
would create an array of size five i.e.
this
is
a
really really long
test
Any ideas?
Thanks!
P
2 5851
You'll need to use RegularExpressions, http://msdn2.microsoft.com/en-us/lib...gex.split.aspx. If you google regex split excel, you'll find code like: - string input = "john,doe,is,\"123 somewhere, suite 4\"";
-
string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
-
string[] items = System.Text.RegularExpressions.Regex.Split(input, pattern);
It's just ugly enough to work, but it splits on commas instead of spaces. I thought that I had a sample that splits on spaces or commas, but I cannot find it. If you google some more, you may find it; otherwise, you'll need a regex expert, not me.
You'll need to use RegularExpressions, http://msdn2.microsoft.com/en-us/lib...gex.split.aspx. If you google regex split excel, you'll find code like: - string input = "john,doe,is,\"123 somewhere, suite 4\"";
-
string pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
-
string[] items = System.Text.RegularExpressions.Regex.Split(input, pattern);
It's just ugly enough to work, but it splits on commas instead of spaces. I thought that I had a sample that splits on spaces or commas, but I cannot find it. If you google some more, you may find it; otherwise, you'll need a regex expert, not me.
Found my code, it's the same as what I posted, so it does not split on spaces. Sorry.
You also might want to consider what you want to to do with "a""b"
Excel parses this to a single cell containing a"b
Sign in to post your reply or Sign up for a free account.
Similar topics
by: afrinspray |
last post by:
I'm writing a function that parses a nested list string that might look
like this:
( "HELLO WORLD!" 1231231 awesome ( 1 2 ) )
I wrote the logic already and it starts by splitting the string by...
|
by: oliver |
last post by:
hi there
i'm experimanting with imaplib and came across stringts like
(\HasNoChildren) "." "INBOX.Sent Items"
in which the quotes are part of the string.
now i try to convert this into a...
|
by: William Stacey [MVP] |
last post by:
Would like help with a (I think) a common regex split example. Thanks for
your example in advance. Cheers!
Source Data Example:
one "two three" four
Optional, but would also like to...
|
by: Senthil |
last post by:
Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";
string Line2 = Line.Replace("\",\"","\"\",\"\"");
string CSVColumns = Line2.Split("\",\"".ToCharArray());
|
by: John Salerno |
last post by:
This is an example in the book I'm reading:
string fullName = " Edward C Koop ";
fullName = fullName.Trim();
string names = fullName.Split(' ');
string firstName = names; //...
|
by: mannyGonzales |
last post by:
Hey guys,
Earliery I posted this common task of reading a csv file.
My data read as: "1","2","3"
Unfortunately it now reads as:
"1","Text with, comma", "2"
embedded commas!...
|
by: lgbjr |
last post by:
Hello All,
I have the following type of string: "X:Y\Z.exe" "123"
What I need is an array of strings with the information from within each set
of quotes. I was trying to use a Regex.Split, but...
|
by: tom t/LA |
last post by:
Here is a function to convert a CSV file to a Javascript array. Uses
idealized file reading functions based on the std C library, since
there is no Javascript standard. Not fully tested.
...
|
by: Robert Dodier |
last post by:
Hello,
I'd like to split a string by commas, but only at the "top level" so
to speak. An element can be a comma-less substring, or a
quoted string, or a substring which looks like a function...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |