Removing all non-numerics before the first and last digit

Hello!
I’m trying to figure out a foolproof way of removing / trimming / match and replace, all character that are not digits before the first occurance of a digit and after the last occurance.
Example matching:

123-asd-123
asd-123.asd
54-asd-23-tg1hello

Maybe you are looking for this:


grafik

strPattern = "^\D+|\D+$"
 "^\D+|\D+$"
 strText = "54-asd-23-tg1hello"
 "54-asd-23-tg1hello"
 System.Text.RegularExpressions.Regex.Replace(strText, strPattern,"")
 "54-asd-23-tg1"

1 Like

It sure was! I feel a bit dumb that I didnt manage to figure out that regex myself :sweat_smile:
I appreciate the help very much thank you!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.