C # - regex for separating a string into spaces if no double quote occurs

This thread is very similar to what I want: A regular expression to be separated by spaces, if only in quotation marks

But I need a few additional rules that I can’t understand: - the above thread is divided into spaces if they are not in double quotes. However, it also breaks down into punctuation. I need something inside double quotes to stay as one entity.

For example:
/ Update setting0 value = "new value" / Save should return
/ Update
setting0
Value =
new value (I don’t care if it disables quotes or not)
/ Save

/ Import "C: \ path \ file.xml" "C: \ path_2 \ file_2.xml" / The output should return / Import
C: \ path \ file.xml (I don't care if it disables quotes or not)
C: \ path_2 \ file_2.xml
/ Exit

I ended up using this expression from the stream above:

(?<=")\w[\w\s]*(?=")|\w+|"[\w\s]*"

Can someone please help me set it up? Thank!

+5
source share
5 answers

I have not tried this in C #, but VBA in Excel, but it can be useful. I also changed the double to one quote. Anyway, this is a regular expression

Text

/ Update setting0 value = 'new value' / Save

Regexp:

('{1}(\w|\s|:|\\|\.)+'{1}|\w)+

Result:

Update

setting0

Value

"new meaning"

Save

Text

/ 'C:\path\file.xml' 'C:\path_2\file_2.xml'/Exit

:

"C:\\file.xml

"C:\path_2\file_2.xml

+3

, . , , , , , :

public static string[] ParseLine(string line)
        {
            var insideQuotes = false;

            var parts = new List<string>();

            var j = 0;

            for (var i = 0; i < line.Length; i++)
            {
                switch (line[i])
                {
                    case '"':
                         insideQuotes = !insideQuotes;
                         break;
                    case ' ':
                         if (!insideQuotes)
                         {
                             parts.Add(line.Substring(j, i - j));
                             j = i + 1;
                         }
                         break;
                    default:
                         continue;
                }
            }

            return parts.ToArray();
        }

, , .

0

, :

^
\s*
(?:
    (?:
        ([^\s"]+)
        |
        "([^"]*)"
    )
    \s*
)+
$
0
var matches = Regex.Matches("/Update setting0 value=\"new value\" /Save", "\\G(?:(\"[^\"]*\"?|[^ \"]+)|[ ]+)");

foreach (Match match in matches) {
    foreach (Capture capture in match.Groups[1].Captures) {
        Console.WriteLine(capture);
    }
}

( "new value" new value)

var matches = Regex.Matches("/Update setting0 value=\"new value\" /Save", "\\G(?:\"(?<1>[^\"]*)\"?|(?<1>[^ \"]+)|[ ]+)");

? \" , .

0

, eulerfx . :

, ( " " ).

,

, ,

, .

IMAP.

    public static string[] ParseLine(string line)
    {
        var insideQuotes = false;
        var start = -1;

        var parts = new List<string>();

        for (var i = 0; i < line.Length; i++)
        {
            if (Char.IsWhiteSpace(line[i]))
            {
                if (!insideQuotes)
                {
                    if (start != -1)
                    {
                        parts.Add(line.Substring(start, i - start));
                        start = -1;
                    }
                }
            }
            else if (line[i] == '"')
            {
                if (start != -1)
                {
                    parts.Add(line.Substring(start, i - start));
                    start = -1;
                }
                insideQuotes = !insideQuotes;
            }
            else
            {
                if (start == -1)
                    start = i;
            }
        }

        if (start != -1)
            parts.Add(line.Substring(start));

        return parts.ToArray();
    }
0

All Articles