Gaz83
Gaz83

Reputation: 2875

Using Regex, how to find repeating patterns between 2 characters?

How an I use regex to find anything between 2 ASCII codes? ASCII code STX (\u0002) and ETX (\u0003)

Example string "STX,T1,ETXSTX,1,1,1,1,1,1,ETXSTX,A,1,0,B,ERRETX"

Using Regex on the above my matches should be

,T1,
,1,1,1,1,1,1,
,A,1,0,B,ERR

Did a bit of googling and I tried the following pattern but it didn't find anything.

@"^\u0002.*\u0003$"

UPDATE: Thank you all, some great answers below and all seem to work!

Upvotes: 2

Views: 1040

Answers (3)

Arie
Arie

Reputation: 5373

            var s = "STX,T1,ETXSTX,1,1,1,1,1,1,ETXSTX,A,1,0,B,ERRETX";
            s = s.Replace("STX", "\u0002");
            s = s.Replace("ETX", "\u0003");

            var result1 = Regex.Split(s, @"[\u0002\u0003]").Where(a => a != String.Empty).ToList();
            result1.ForEach(a=>Console.WriteLine(a));

            Console.WriteLine("------------ OR WITHOUT REGEX ---------------");

            var result2 = s.Split(new char[] { '\u0002','\u0003' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            result2.ForEach(a => Console.WriteLine(a));

output:

,T1,
,1,1,1,1,1,1,
,A,1,0,B,ERR
------------ OR WITHOUT REGEX ---------------
,T1,
,1,1,1,1,1,1,
,A,1,0,B,ERR

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626754

You may use a non-regex solution, too (based on Wyatt's answer):

var result = input.Split(new[] {'\u0002', '\u0003'}) // split with the known char delimiters
       .Where(p => !string.IsNullOrEmpty(p)) // Only take non-empty ones
       .ToList();

enter image description here

A Regex solution I suggested in comments:

var res = Regex.Matches(input, "(?s)\u0002(.*?)\u0003")
          .OfType<Match>()
          .Select(p => p.Groups[1].Value)
          .ToList();

Upvotes: 3

Wyatt Earp
Wyatt Earp

Reputation: 1823

You could use Regex.Split.

var input = (char)2 + ",T1," + (char)3 + (char)2 + ",1,1,1,1,1,1," + (char)3 + (char)2 + ",A,1,0,B,ERR" + (char)3;
var result = Regex.Split(input, "\u0002|\u0003").Where(r => !String.IsNullOrEmpty(r));

Upvotes: 4

Related Questions