1

I am trying to using regex replace in C# although I am having some issues getting the pattern to align correctly. What I am looking to do is replace certain combinations as per the first 2 groups but not if it matches the pattern of the 3rd group. What I have so far is

 var pattern = @"(,)|(\[\{)|(^:\[[*]])";

 string NewLineValue = Regex.Replace(LineValue, pattern,Environment.NewLine);

Essentially I want to replace all comma's or [{ combinations in a string but not if the comma appears within [[]] characters (e.g [[1234,5678]])

Any help is much appreciated...

5
  • The character "^" when placed inside square brackets is used to make a negated character class. The way you used it, in the third group, will only mean that it's the beginning of the text. Commented Dec 24, 2018 at 15:18
  • 2
    Sounds like you wanted to parse a JSON file..... Commented Dec 24, 2018 at 15:25
  • 1
    An short input example and desired output could help. Also, in case you're not trying to parse a JSON file like @CamiloTerevinto guessed (you could have it easier, if you are), try using a negative lookahead. See: regular-expressions.info/lookaround.html Commented Dec 24, 2018 at 15:33
  • @CamiloTerevinto that is the end goal, however at the moment I am trying to put some of the entries into a readable format for the end user to determine what elements are required.... the line items in the file do not follow a consistent format either as they are activity log entries and change line by line. Commented Dec 24, 2018 at 17:13
  • @shwartz, thank you. would you be able to elaborate as to where the "^" symbol would need to be placed in order to ignore the "," if it appears within a [[x]] section? your advice is much appreciated. Commented Dec 24, 2018 at 17:15

1 Answer 1

4

You need to match and capture all substrings between [[ and ]] (to be restored in the resulting string) and then match , or [{ in all other contexts to replace with a newline:

var pattern = @"(?s)(\[\[.*?]])|,|\[{";
var result = Regex.Replace(s, pattern, m => 
    m.Groups[1].Success ? m.Groups[1].Value : Environment.NewLine);

The pattern matches:

  • (?s) - a RegexOptions.Singleline inline option
  • (\[\[.*?]]) - Group 1: [[, any 0+ chars, as few as possible, and then ]]
  • | - or
  • , - a comma
  • | - or
  • \[{ - [{ substring.

If Group 1 matches (m.Groups[1].Success), the match is pasted back (m.Groups[1].Value), else, the match (, or [{) is replaced with a Environment.NewLine.

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.