samithagun
samithagun

Reputation: 693

Separate Characters and Numbers from a String, vb.net

I have a string like this. AB24

I need to get AB separately and 24 separately. Using Regex will be fine.

I already used,

Regex.Match("AB24", "\d+$").Value

to get 24 out.

Now I need to get AB out? Please help me..

Upvotes: 2

Views: 3879

Answers (3)

hwnd
hwnd

Reputation: 70722

Use a capture group ( ) to separate your matches.

Dim m As Match = Regex.Match("AB24", "^([A-Z]+)([0-9]+)$")

If (m.Success) Then
   Console.WriteLine(m.Groups(1).Value)
   Console.WriteLine(m.Groups(2).Value)
End If

Output

AB
24

Upvotes: 3

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

You can use regex to Seperate Number

Regex.Replace("AB24", "(?:[0-9]+\.?[0-9]*|\.[0-9]+)", "")

You can use regex to Seperate Text

Regex.Replace("AB24", "[^\A-Z]", "")

Upvotes: 1

Indranil.Bharambe
Indranil.Bharambe

Reputation: 1498

you can simply use lastNo = Regex.Match(txtNextLot.Text, "/^[A-z]+$/").Value

Upvotes: 0

Related Questions