Hendry
Hendry

Reputation: 900

Regex: Find where letters end and numbers start

I have strings like MA14 or MD22b and I have to find the index between MAand 14.

Upvotes: 0

Views: 45

Answers (2)

m.cekiera
m.cekiera

Reputation: 5395

Try with:

(?<=[A-Z])(?=\d)|(?<=\d)(?=[A-Z])

DEMO

ind demo the () for group capturing is added, to display indices on which it match.

Upvotes: 2

rbm
rbm

Reputation: 3253

Simple regex:

string s = "ABC142";    
var r= new Regex("^([A-Za-z]*)(\\d*)$");
var m = r.Match(s);
m.Groups[1].Index.Dump();  # index of the match
m.Groups[1].ToString().Dump();
m.Groups[2].Index.Dump(); # index of the match
m.Groups[2].ToString().Dump();

will print

0
ABC
3
142

(Dump is from LINQPAd, you can use Console.WriteLine etc).

Upvotes: 0

Related Questions