Android Question Extract Number from alphanumeric string

emexes

Expert
Licensed User
https://regex101.com/ is good for exploring regular expressions (and for building them, too ;-) eg:

upload_2019-6-6_0-57-48.png
 
Upvote 0

trepdas

Active Member
Licensed User
B4X:
Dim s As String = "AND051"
Dim m As Matcher = Regex.Matcher("\d+", s)
If m.Find Then
   Dim number As Int = m.Match
   Log(number)
End If


How would I delete the numbers from the strings and keep only the "AND" ?
B4X:
oldstring = "AND051"
newstring = "AND"
 
Upvote 0

Quandalle

Member
Licensed User
In your case, you can also use the replace function
B4X:
    Dim s As String = "AND051"
    Log(Regex.Replace("\D",s,"")) '  replace all non digit char by nothing -> "051"
    Log(Regex.Replace("\d",s,"")) ' replace all digit char by nothing -> "AND"
n
 
Upvote 0
Top