Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write regex for "123456#" to print 123456?

Tags:

c#

regex

Hi I need a regex to replace # or * with ""(emptyString) I have tried with /[\\*\\#]/g but does not seem to be working.

http://ideone.com/MtjsX5

please need your help in this.

I am actually using this a Grxml grammar as below

SWI_meaning          = DIGITS.SWI_literal.replace( /[ ]+/g, '' );
 SWI_meaning          = SWI_meaning.replace( /[\*\#]/g, '' );

Thanks

like image 232
GuruKulki Avatar asked Nov 16 '25 14:11

GuruKulki


2 Answers

Rather than regex, you can use char.IsDigit to filter out only digits from the string. Try the following.

string str = "123456#";
string newString = string.Join("",
                         str.Select(r=> char.IsDigit(r) ? r.ToString():""));

EDIT: courtesy @L.B

string newString = String.Join("",str.Where(char.IsDigit));
like image 168
Habib Avatar answered Nov 19 '25 04:11

Habib


string str = "123456#";
string clean = Regex.Replace(str, @"[#*]", string.Empty);
like image 30
Jack Hughes Avatar answered Nov 19 '25 04:11

Jack Hughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!