How to replace all the words in string that contains only numbers?

I am looking for a solution to get a string out of an input string where in my output string will only contain the words from the sentence where only numbers are present
Eg- B12 Live 3400 45 00 He12
Output- B12 Live He12

Hi,

This is how I would do it (it might not be optimal or efficient but a solutions nonetheless)

  1. Split the string using space
  2. Run loop an concatenate each element of the array with a space using Regex.IsMatch where the condition should specify that the array element has alphabets and/or numbers, and elements having only numbers should be set to null and not concatenated at all.

Hope this helps.

If you dont know regex, I will be posting the solution shortly.

@Soumyodip_s

strOutput = string.Join(" “,System.Text.RegularExpressions.Regex.Matches(strInput,”[a-zA-Z]{1,}[0-9]{1,}"))

Regards,
Mahesh

2 Likes