I’am trying to filter a string using Regex, I want to filter the PÑ combination out of my string, and also some Seperate P‘s, However in the string that gets provided are also some parts of text like: PB00121324, I want to keep that P.
this is the string:
PB014EC8F;1359300102;NL1200000001 ;Ey³ PÑ PÑ B014EC8F;1359 P B014EC8F;1359
I want to filter out: seperate P‘s and PÑ‘s
So that the output would be this:
PB014EC8F;1359300102;NL1200000001 ;Ey³
I’am now using this code:
string CleanString = Regex.Replace(DirtyString, @"[\\PÑ?]", "");
The problem is that it will result this:
B014EC8F;1359300102;NL1200000001 ;Ey³
instead of this:
PB014EC8F;1359300102;NL1200000001 ;Ey³
Does anybody know the Regex for this?
Thanks in advance!
You can use
\bto find a word boundary so for example the regex\bP\bwill find aPin isolation.With
\bP\b…Input
Hello P Goodbye->PMatches as it is adjacent to spaces (non-word characters)Input
HelloP Goodbye->Pdoes not match as it is adjacent to the previous wordInput
Hello.P.Hello->Pmatches as it is adjacent to non-word charactersInput
P Hello->Pmatches as it is adjacent to the start of the string and a space.In response to your comment below – If the first regex
[..]is intended to find two full-stops/periods then this is not what it will do.[]denotes a character class and providing duplicate characters in a character class is meaningless. To match two full-stops use\.\.I’m not sure if combining into one regex is possible as the order of execution is very important…
Hello..P..Sis a P in isolation. However after removal of..we haveHelloPSwhich is not a P in isolation. To combine multiple replace is easy but they will all effectively happen ‘at the same time’ whereas your current method is doing one, then the next, then the next. ie – The input is different on each occasion.However to combine the replaces you would do…
Sort of demo