Ian
Ian

Reputation: 30823

Parse string with whitespace and quotation mark (with quotation mark retained)

If I have a string like this

create myclass "56, 'for the better or worse', 54.781"

How can I parse it such that the result would be three string "words" which have the following content:

[0] create
[1] myclass
[2] "56, 'for the better or worse', 54.781"

Edit 2: note that the quotation marks are to be retained

At first, I attempted by using string.Split(' '), but I noticed that it would make the third string broken to few other strings.

I try to limit the Split result by using its count argument as 3 to solve this. And is it ok for this case, but when the given string is

create myclass false "56, 'for the better or worse', 54.781" //or
create myclass "56, 'for the better or worse', 54.781" false

Then the Split fails because the last two words will be combined.

I also created something like ReadInBetweenSameDepth to get the string in between the quotation mark

Here is my ReadInBetweenSameDepth method

//Examples:
    //[1] (2 + 1) * (5 + 6) will return 2 + 1
    //[2] (2 * (5 + 6) + 1) will return 2 * (5 + 6) + 1
public static string ReadInBetweenSameDepth(string str, char delimiterStart, char delimiterEnd) {
  if (delimiterStart == delimiterEnd || string.IsNullOrWhiteSpace(str) || str.Length <= 2)
    return null;
  int delimiterStartFound = 0;
  int delimiterEndFound = 0;
  int posStart = -1;
  for (int i = 0; i < str.Length; ++i) {
    if (str[i] == delimiterStart) {
      if (i >= str.Length - 2) //delimiter start is found in any of the last two characters
        return null; //it means, there isn't anything in between the two
      if (delimiterStartFound == 0) //first time
        posStart = i + 1; //assign the starting position only the first time...
      delimiterStartFound++; //increase the number of delimiter start count to get the same depth
    }
    if (str[i] == delimiterEnd) {
      delimiterEndFound++;
      if (delimiterStartFound == delimiterEndFound && i - posStart > 0)
        return str.Substring(posStart, i - posStart); //only successful if both delimiters are found in the same depth
    }
  }
  return null;
}

But though this function is working, I found it pretty hard to combine the result with the string.Split to make the correct parsing as I want.

Edit 2: In my poor solution, I need to re-add the quotation marks later on

Is there any better way to do this? If we use Regex, how do we do this?

Edit:

I honestly am unaware that this problem could be solved the same way as the CSV formatted text. Neither did I know that this problem is not necessarily solved by Regex (thus I labelled it as such). My sincere apology to those who see this as duplicate post.

Edit 2:

After working more on my project, I realized that there was something wrong with my question (that is, I did not include quotation mark) - My apology to the previously best answerer, Mr. Tim Schmelter. And then after looking at the dupe-link, I noticed that it doesn't provide the answer for this either.

Upvotes: 7

Views: 420

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460328

I would use a real csv-parser for this task. The only one available in the framework is the TextFieldParser-class in the VisualBasic namespace:

string str = "create myclass \"56, 'for the better or worse', 54.781\"";
var allLineFields = new List<string[]>();
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(str)))
{
    parser.Delimiters = new string[] { " " };
    parser.HasFieldsEnclosedInQuotes = true;  // important
    string[] lineFields;
    while ((lineFields = parser.ReadFields()) != null)
    {
        allLineFields.Add(lineFields);
    }
}

Result:

enter image description here

But there are others available like this or this.

Upvotes: 1

Tushar
Tushar

Reputation: 87233

Regex Demo

(\w+|"[^"]*")

Get the matches in the first capture group.

  1. \w+: Matches alphanumeric characters and underscore one or more times
  2. "[^"]*": Matches anything that is wrapped in double quotes
  3. |: OR condition in regex

Upvotes: 2

vks
vks

Reputation: 67988

You can split by this

\s(?=(?:[^"]*"[^"]*")*[^"]*$)

See demo.

https://regex101.com/r/fM9lY3/60

string strRegex = @"\s(?=(?:[^""]*""[^""]*"")*[^""]*$)";
Regex myRegex = new Regex(strRegex, RegexOptions.Multiline);
string strTargetString = @"create myclass ""56, 'for the better or worse', 54.781""";

return myRegex.Split(strTargetString);

Upvotes: 3

Related Questions