4

Input string:

[Wsg-Fs]-A-A-A-Cgbs-Sg7-[Wwg+s-Fs]-A-A-Afk-Cgbs-Sg7

Desired output is a string array:

[Wsg-Fs] A A A Cgbs Sg7 [Wwg+s-Fs] A A Afk Cgbs Sg7

If I split the input string with - as delimiter, the string within square brackets also gets split.

How do I split the string such that - within square bracket should be ignored?

I could find some similar posts trying to ignore delimiter enclosed in quotes, however I have not been able to apply those solution to my problem.

Any suggestion would be really helpful. Thanks!

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Aqua267
  • 873
  • 3
  • 12
  • 35
  • This post shows how to do it with CSV files having quotation marks; I imagine it could be adapted for square brackets: http://www.kimgentes.com/worshiptech-web-tools-page/2008/10/14/regex-pattern-for-parsing-csv-files-with-embedded-commas-dou.html – Robert Harvey Aug 30 '12 at 20:10

2 Answers2

9

Assuming there are no nested square brackets, you can use the following to only match - characters that are outside of square brackets:

-(?![^\[]*\])

Example: http://regex101.com/r/sX5hZ2

This uses a negative lookahead, with the logic that if there is a closing square bracket before any opening square brackets, then the - that we tried to match is inside of brackets.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
2

Resurrecting this ancient question to offer another solution, since the current one only checks that a splitting - is not followed by a ], which does not guarantee that it is enclosed in brackets.

\[[^\]]*\]|(-)

Then splitting on Group 1 (see the Group 1 captures in the bottom right panel of the demo)

To split on Group 1, we first replace Group 1 with something distinctive, such as SPLIT_HERE, then we split on SPLIT_HERE

using System;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
class Program
{
static void Main() {
string s1 = @"[Wsg-Fs]-A-A-A-Cgbs-Sg7-[Wwg+s-Fs]-A-A-Afk-Cgbs-Sg7";
var myRegex = new Regex(@"\[[^\]]*\]|(-)");
var group1Caps = new StringCollection();

string replaced = myRegex.Replace(s1, delegate(Match m) {
if (m.Groups[1].Value == "") return m.Groups[0].Value;
else return "SPLIT_HERE";
});

string[] splits = Regex.Split(replaced,"SPLIT_HERE");
foreach (string split in splits) Console.WriteLine(split);

Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();

} // END Main
} // END Program

Here's a full working online demo

Reference

How to match pattern except in situations s1, s2, s3

How to match a pattern unless...

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105