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);
4
  • 1
    As noted by Luuk, the regex you've provided works with the text you've provided. Please provide a minimal reproducible example that demonstrates the problem. Commented May 18, 2019 at 9:50
  • @jonskeet The regex is used here: github.com/SMLHelper/SMLHelper/blob/LangOverrideBug/SMLHelper/… As for a mcve, I've updated my original question Commented May 18, 2019 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. Commented May 18, 2019 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. Commented May 18, 2019 at 10:36

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.