1

I'm trying to parse a file's contents using regex, but the patter I came up with doesn't seem to work.

The regex I am trying to use is

string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";

and the text i am trying to parse it against is

string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";

However, it returns 0 results

MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);

I have tried testing this regex on RegExr, which worked as expected.
I am unsure why this doesn't work when trying it in C#.

MCVE:

string regex = @"^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$";
string text = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:{val-u{0}e}
key:{val__[!]
-u{0}{1}e}";
MatchCollection matches = Regex.Matches(text, regex, RegexOptions.Multiline);
Console.WriteLine(matches.Count);
Emma
  • 27,428
  • 11
  • 44
  • 69
AlexejheroYTB
  • 73
  • 1
  • 1
  • 18
  • 1
    As noted by Luuk, the regex you've provided works with the text you've provided. Please provide a [mcve] that demonstrates the problem. – Jon Skeet May 18 '19 at 09:50
  • @jonskeet The regex is used here: https://github.com/SMLHelper/SMLHelper/blob/LangOverrideBug/SMLHelper/Patchers/LanguagePatcher.cs#L107-L128 As for a mcve, I've updated my original question – AlexejheroYTB May 18 '19 at 10:26
  • 1
    Right - that code prints 1, not 0. So please provide an example that *actually* prints 0... or if you're in an unusual environment (which might have a broken regex implementation) please provide details of that. – Jon Skeet May 18 '19 at 10:29
  • 1
    At least, it prints 1 on my Windows machine, using CR/LF line breaks. If you change it to just LF line breaks, it prints 5. – Jon Skeet May 18 '19 at 10:36

1 Answers1

3

One way we might like to try is to test that if our expression would be working in another language.

Also, we might want to simplify our expression:

^(.*?)([\s:]+)?{([\s\S].*)?.$

where we have three capturing groups. The first and third ones are our desired key and values.

enter image description here

RegEx

You can modify/simplify/change your expressions in regex101.com.

RegEx Circuit

You can also visualize your expressions in jex.im:

enter image description here

JavaScript Demo

const regex = /^(.*?)([\s:]+)?{([\s\S].*)?.$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:   {val-u{0}e}
key:  {val__[!]
-u{0}{1}e}`;
const subst = `$1,$3`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

C# Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"^(.*?)([\s:]+)?{([\s\S].*)?.$";
        string substitution = @"$1,$3";
        string input = @"key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:   {val-u{0}e}
key:  {val__[!]
-u{0}{1}e}";
        RegexOptions options = RegexOptions.Multiline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
    }
}

Original RegEx Test

const regex = /^(?<key>\w+?)\s*?:\s*?{(?<value>[\s\S]+?)}$/gm;
const str = `key:{value}
key:{valu{0}e}
key:{valu
{0}e}
key:   {val-u{0}e}
key:  {val__[!]
-u{0}{1}e}`;
const subst = `$1,$2`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

References:

Emma
  • 27,428
  • 11
  • 44
  • 69