1

I have the following string

{token1;token2;token3@somewhere.com;...;tokenn}

I need a Regex pattern, that would give a result in array of strings such as

token1
token2
token3@somewhere.com
...
...
...
tokenn

Would also appreciate a suggestion if can use the same pattern to confirm the format of the string, means string should start and end in curly braces and at least 2 values exist within the anchors.

2 Answers2

0

You may use an anchored regex with named repeated capturing groups:

\A{(?<val>[^;]*)(?:;(?<val>[^;]*))+}\z

See the regex demo

  • \A - start of string
  • { - a {
  • (?<val>[^;]*) - Group "val" capturing 0+ (due to * quantifier, if the value cannot be empty, use +) chars other than ;
  • (?:;(?<val>[^;]*))+ - 1 or more occurrences (thus, requiring at least 2 values inside {...}) of the sequence:
    • ; - a semi-colon
    • (?<val>[^;]*) - Group "val" capturing 0+ chars other than ;
  • } - a literal }
  • \z - end of string.

.NET regex keeps each capture in a CaptureCollection stack, that is why all the values captured into "num" group can be accessed after a match is found.

C# demo:

var s = "{token1;token2;token3;...;tokenn}";
var pat = @"\A{(?<val>[^;]*)(?:;(?<val>[^;]*))+}\z";
var caps = new List<string>();
var result = Regex.Match(s, pat);
if (result.Success)
{
    caps = result.Groups["val"].Captures.Cast<Capture>().Select(t=>t.Value).ToList();
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I am afraid, this is not working, can you paste the result of the split for {Teddy;Bear;Giant;Gorilla} Lion? Someone deleted the answer where I pasted that example (sorry for that) –  Dec 06 '16 at 15:22
  • By the way, to ensure at least 2 value, can't we use quantifier like {2,} –  Dec 06 '16 at 15:26
  • It is working well. You do not need any limiting quantifiers. I posted the code necessary to obtain the values from the CaptureCollection. – Wiktor Stribiżew Dec 06 '16 at 15:27
  • this is the exception I get using the pattern: Additional information: parsing "\A{(?< val >[^;]*)(?:;(?[^;]*))+}\z" - Invalid group name: Group names must begin with a word character. Ah sorry, wait, my bad, didn't noticed the spaces –  Dec 06 '16 at 15:32
  • 1
    I wan't to use Regex.Match only to confirm the input string format, and Regex.Split to get array straight forward. Is this even possible or am I beating about a bush? ;) If I had to go into explicit looping, then I would have never asked this question –  Dec 06 '16 at 15:39
  • You do not need to loop. You get all capture values with 1 line using LINQ. As I said, you can both *validate* and *get* all the necessary values with sole regex in C#. – Wiktor Stribiżew Dec 06 '16 at 16:17
0

Read it(similar to your problem): How to keep the delimiters of Regex.Split?.

For your RegEx testing use this: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.

But RegEx is a very resource-intensive, slow operation.

In your case will be better to use the Split method of string class, for example : "token1;token2;token3;...;tokenn".Split(';');. It will return to you a collection of strings, that you want to obtain.

Community
  • 1
  • 1
EgoPingvina
  • 734
  • 1
  • 10
  • 33