4

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 :(

3
  • 1
    Have you tried String.Replace ? Commented Aug 19, 2015 at 8:04
  • no i have not tried, but the program i wrote is based on that regex and the data is dynamically changing the admin word can change to different index. Commented Aug 19, 2015 at 8:08
  • Kevin, so what? string.Replace would still work.. Commented Aug 19, 2015 at 8:10

5 Answers 5

4

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);
Sign up to request clarification or add additional context in comments.

3 Comments

hello, what if i have two admins 1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin|Admin|||P|1|20150511083525
hello there is just one problem if the characters have space it should replace space with |
use this ([^\w]*Admin[^\w]*)+|[|\\^&\r\n\s]+
4

Not necessary to use regex here. You can use Replace function;

yourString.Replace("Admin|", string.Empty);

If using Regex is important for you - try this variant:

Regex r = new Regex(@"\bAdmin|\b");
var output = r.Replace(s, string.Empty);

2 Comments

Or even yourString.Replace("|Admin|", "||"); if "Admin" were to occur in other places.
Won't remove "empty spaces" - || - which is expected if you see the desired output.
1

100% working

 Regex.Replace(@"1H|\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin||||P|1|20150511083525  1D", "Admin", "",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

changes :

1: @ before string and

2:System.Text.RegularExpressions.RegexOptions.IgnoreCase

1 Comment

As (kind of) said in another comment, change the regex (second argument - Admin) to \|Admin\|' to remove the vertical bar as well. Then add |\||` to remove "empty spaces" and exchange with |. I.e. "\|Admin\||\||" as regex (2nd argument) and "|" as replacement string (3rd argument). It's not perfect because if Admin, or an empty space, is in the begining or end of the string, it won't work. It's possible to fix, but if it isn't necessary, I don't want to complicate it further ;)
1

You can do this simply via

\|*Admin\|*

You can remove using this regex and replace by |. See demo.

https://regex101.com/r/uF4oY4/81

Comments

1

Here is a 2 step solution: use your regex (with | added to the negated character class) and \bAdmin\b as an alternative (in your regex, it was not treated as a sequence of characters, but individual character a, d..., \b inside a character class matches a backspace symbol, not a word boundary) to replace with | and then join the string back:

var str = "1H|\\^&|||ARCHITECT^8.10^F3453010030^H1P1O1R1C1Q1L1|||Admin||||P|1|20150511083525\r\n1D";
var res = string.Join("|", Regex.Replace(str, @"[^|\w.-]+|\bAdmin\b", "|").Split(new[] {"|"}, StringSplitOptions.RemoveEmptyEntries));

Result: enter image description here

Update

You can also replace that all with one pass using the following regex:

\W*Admin\W*|[\\|^&\s]+

See demo

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.