简体   繁体   中英

How to replace more than one special character from a string

I have a string ie as given below.

 string s = @"Test1212ULL99|H|||F~~~
NTE|||      Reference Range~  Negative~  Elevated Antibody Level~~
FT1||||2015072100||NA|^^DMHC100^STDNULL99^I9
MSH|^~\&|Cnoss CA||||901834|P|2.4|||NE|NE|||||CALINX_1.3~
PID|2||MRN45161510^^^^PT~1860664CASANOVA AVE&820^^MONTEREY^CA^93940^^H||(831) 917-1541";

string replacementString = "\r";
 string result = Regex.Replace(s, @"~~\r", replacementString);

I have to replace all the ~ sign from last of row only not from mid of the row. Eg line 1 contains three ~ sign in last and line two and three contains 2 and 1 ~ sign in last.

Can any one tell me how i can achieve this? Any help will be appreciated.

~+(?=[\r\n])

This will replace ~ from the end of string only.Replace by empty string .

See demo.

https://regex101.com/r/sS2dM8/13

Short and Sweet. (If you just need substitution and not capturing ~ )

~+?$

This will remove all the ~ from last of line only and not from middle. Works for multiline string as well.

Demo: https://regex101.com/r/cP8lG0/1

Cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM