redcabbage
redcabbage

Reputation: 31

How to extract string two, one in between brackets and two not in brackets?

I'm trying to figure out a way to use regular expressions to extract a string into two values. An example string is "Regional Store 1 - Madison [RSM1]" "Regional Store 2 [SS2]"

and I would like to have them extracted to "Regional Store 1 - Madison", "RSM1" and "Regional Store 2", "SS2".

I've tried using the regular expression (?<=[)(.*?)(?=]) but it gives me "Regional Store 2 [", "SS2", "]". Other regular expressions I've tried give me "Regional Store 2", "[SS2]".

Since the strings will always follow the format of "{Name} [{code}]" I'm wondering if I should just be using string.split instead.

Upvotes: 1

Views: 158

Answers (2)

AJP
AJP

Reputation: 43

please try this pattern

MatchCollection mcCollection = Regex.Matches(sIdentifire, @"(Regional\s+Store\s+.+?\[.+?\])");

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626932

You may solve the problem without regex by trimming off the trailing ] and splitting with "[" or " [":

var s = "Regional Store 1 - Madison [RSM1]";
var chunks = s.TrimEnd(']').Split(" [");
Console.WriteLine("Name={0}, Code={1}", chunks[0], chunks[1]);
// => Name=Regional Store 1 - Madison, Code=RSM1

Or, with a regex:

var pattern = @"^(.*?)\s*\[([^][]*)]$";
chunks = Regex.Match(s, pattern)?.Groups.Cast<Group>().Skip(1).Select(x => x.Value).ToArray();
Console.WriteLine("Name={0}, Code={1}", chunks[0], chunks[1]);
// => Name=Regional Store 1 - Madison, Code=RSM1

See the C# demo and the regex demo.

Pattern details

  • ^ - start of string
  • (.*?) - Group 1: any zero or more chars other than a newline char, as few as possible
  • \s* - zero or more whitespace chars
  • \[ - a [ char
  • ([^][]*) - Group 2: any zero or more chars other than [ and ]
  • ]$ - a ] char and end of string.

Upvotes: 1

Related Questions