Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...