Sebastian
Sebastian

Reputation: 33

REGEX Expression C#. Split string by whitespace outside the quotation marks

I'm trying to define a regular expression for the Split function in order to obtain all substring split by a whitespace omitting those whitespaces that are into single quotation marks.

Example:

key1:value1 key2:'value2 value3'

i Need these separated values:

I'm tried to perform this in different ways:

What i am wrong with this code?

Could you please help me with this?

Thanks in advance

Upvotes: 1

Views: 716

Answers (3)

LetzerWille
LetzerWille

Reputation: 5658

    var st = "key1:value1 key2:'value2 value3'";
     var result = Regex.Matches(st, @"\w+:\w+|\w+:\'[^']+\'");

    foreach (var item in result)
        Console.WriteLine(item);

The result should be:

key1:value1
key2:'value2 value3'

Upvotes: 0

jdweng
jdweng

Reputation: 34421

Try following :

       static void Main(string[] args)
        {
            string input = "key1:value1 key2:'value2 value3'";
            string pattern = @"\s*(?'key'[^:]+):((?'value'[^'][^\s]+)|'(?'value'[^']+))";

            MatchCollection matches = Regex.Matches(input, pattern);
            foreach (Match match in matches)
            {
                Console.WriteLine("Key : '{0}', Value : '{1}'", match.Groups["key"].Value, match.Groups["value"].Value);
            }
            Console.ReadLine();
        }

Upvotes: 0

linden2015
linden2015

Reputation: 887

A working example:

(\w+):(?:(\w+)|'([^']+)')

(\w+)       # key: 1 or more word chars (captured)
:           # literal
(?:         # non-captured grouped alternatives
(\w+)       # value: 1 or more word chars (captured)
|           # or
'([^']+)'   # 1 or more not "'" enclosed by "'" (captured)
)           # end of group

Demo

Your try:

(\s)^('\s')

^ means beginning of line, \s is a white-space characters. If you want to use the not-operator, this only works in a character class [^\s] -> 1 character not a white-space.

Upvotes: 2

Related Questions