Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex remove word from a specific string

Tags:

c#

regex

I have tried to remove a the "Admin" word in this string

1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin||||P|1|20150511083525
1D

with this regex

[^\w\b(Admin\])\b.-]+ 

and the output is 1H|ARCHITECT|8.10|F3453010030|H1P1O1R1C1Q1L1|Admin|P|1|20150511083525|1D

it does not remove the Admin word.

output desired:

1H|ARCHITECT|8.10|F3453010030|H1P1O1R1C1Q1L1|P|1|20150511083525|1D

i need help to improve the regex :(

like image 397
Kevin Rodriguez Avatar asked Sep 05 '25 03:09

Kevin Rodriguez


1 Answers

Basically your regex should be like this

string input = @"1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin||||P|1|20150511083525
    1D";
string pattern = @"([^\w]*Admin[^\w]*)+|[|\\^&\r\n]+";
string replacement = "|";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
like image 52
Arunesh Singh Avatar answered Sep 07 '25 16:09

Arunesh Singh