Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

C# regex get string beetween numbers

I have a simple string that would look like

string text = "1\t2:3|5"

So my goal is to get the chars between numbers something like:

string[] result = {"\t", ":", "|"}

I tried to bite this by getting numbers first

 Regex.Matches(text , "(-?[0-9]+)").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

And then use this regex pattern

/[^ \w]/g

Well I am getting symbols but "\t" is splitted to "" and without "t", how to regex this?

Upvotes: 0

Views: 56

Answers (1)

Genusatplay
Genusatplay

Reputation: 771

You can do it with Regex or String.Split - result is same

char[] separators = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };        
var splitResult = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);        

var matchesResult = Regex.Matches(text, @"(?<=\d)\D+(?=\d)").Select(w => w.Value).ToArray();

https://dotnetfiddle.net/csj1f6

Upvotes: 1

Related Questions