1
votes

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:

  • key1:value1

  • key2:'value2 value3'

I'm tried to perform this in different ways:

  • Regex.Split(q, @"(\s)^('\s')").ToList();
  • Regex.Split(q, @"(\s)(^'.\s.')").ToList();
  • Regex.Split(q, @"(?=.*\s)").ToList();

What i am wrong with this code?

Could you please help me with this?

Thanks in advance

3
Is k1 : v1 k2 : 'v2 v3' a valid input? And if so what output do you expect? - Aleks Andreev
Are you trying to parse JSON? Do not use a regex for this. Use JSON.NET. - Wiktor Stribiżew
@AleksAndreev , no this not a valid input Only this: ---> k1:v1 k2:'v2 v3' only with one space between each parameter In this case, i expect to see: - k1:v1 - k2:'v2 v3' - Sebastian
@WiktorStribiżew no, this is not a JSON is a part of a URL (AWS Cloudsearch parameters) - Sebastian
Is there a working answer below? If yes, please consider accepting. - Wiktor Stribiżew

3 Answers

2
votes

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.

0
votes

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();
        }
0
votes
    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'