0

Not CSV

Sandboxed Enviroment.

I am trying to extract a few "values" from a string. The string will more or less be consistent. I have come to conclusion I will need to rely on regex for this. I can't quite figure out the regex piece.

string str1 = "test \"test1\" \"35\" TestWord Test";
string str2 = "test \"test2\" \"this is a Test\" TestWord2 Test2";

I want my result for each string to basically "split" at each space unless it is in quotes. I know this is not regex, but it is an example of what I want to accomplish.

string[] split = str1.split(' ');

This works fine, for there are no spaces in between the quotes

string[] split = str2.split(' ');

This won't work because there are spaces in between the second quoted portion.

This is why I think regex is the best bet, aside from parsing, the old fashioned way as it were.

I want every piece of extracted data from the string stored into an array.

So for the first example

string[] split = str1.split(' '); 

my result should be

split[0] = test
split[1] = test1
split[2] = 35
split[3] = TestWord
split[4] = Test

and yes I would like to omit any quotes in the results.

Any ideas?

Thank you

Angryjames
  • 21
  • 2
  • 1
    That linked answer has almost nothing to do with the OP question! – Ric Gaudet Jan 18 '16 at 21:17
  • Something like this? It's just `\w+` http://regexr.com/3cjpu – Serguei Fedorov Jan 18 '16 at 21:18
  • @RicGaudet: There is everything OP needs. No need to use a regex here. Use a built-in CSV parser with a space as a separator. `"test \"test2\" \"this is a Test\" TestWord2 Test2"` gets split into `test`, `test2`, `this is a Test`, `TestWord2`, `Test2`. – Wiktor Stribiżew Jan 18 '16 at 21:20
  • @Angryjames: Your strings are parseable with CSV. Add `using Microsoft.VisualBasic.FileIO;`, and this method: `public static string[] ParseCSVString(string csv, string separator) { TextFieldParser parser = new TextFieldParser(new StringReader(csv)); parser.HasFieldsEnclosedInQuotes = true; parser.SetDelimiters(separator); string[] fields = null; while (!parser.EndOfData) fields = parser.ReadFields(); parser.Close(); return fields; }` – Wiktor Stribiżew Jan 18 '16 at 21:36
  • I guess I should mention I am limited because of the sandboxed nature of what i am writing for. I cannot use just any library. – Angryjames Jan 18 '16 at 21:44
  • @Wiktor Stribiżew: I understand that the CSV parser works, but that does not mean the question should be closed just because there is a third party library out there that can do the job. – Ric Gaudet Jan 18 '16 at 21:47
  • @RicGaudet: It is not not a 3rd party, it is a native library that you can use in VS. The same questions have been asked and that solution was always marked as accepted. I am here a year, every day, at regex tag. I know what I am saying. I know regex. **Regex is not the right tool for this job**. – Wiktor Stribiżew Jan 18 '16 at 21:48
  • @Angryjames: Does the sandboxed environment forbids adding `using`s? Note that `string str2 = test "test2" "this is a Test" TestWord2 Test2` does not even compile. Anyway, the regex is already there for several years: [`@"\p{Zs}+(?=[^""]*(?:""[^""]*""[^""]*)*$)"`](https://regex101.com/r/xP7eH9/1). See [*Regex to pick commas outside of quotes*](http://stackoverflow.com/questions/632475/regex-to-pick-commas-outside-of-quotes). – Wiktor Stribiżew Jan 18 '16 at 21:57
  • The Sandbox is limited to certain "libraries / using" Native VB is not one of them as far as i know. – Angryjames Jan 18 '16 at 22:56

0 Answers0