Find and Replace a specific number but not when followed by a specific character
Jesper Bødewadt Møller
I need to replace a number in different strings.
Like replace 3 with 4 but not replace any given number in the “.%X31” only if there is any chracter in front the “3” and not replace the .%X31.
some times .%X31 have 3 in it and some times not.
Hope some one can help out.
@Jesper-Bødewadt-Møller Hi! If you want to replace 3 with 4 whitout affecting .%X31 in this example the solution would be:
Find what: ([#][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z])([0-9])
Replace with: \14
If this doesn’t help you, i don’t know, try to be more specific and post more examples of strings you need to replace.
I would use this strategy :
If we find some digits, right after the syntax
%X, just rewrite this NON wanted block, as it !
If we find other
3digits elsewhere ( which are good candidates ), replace each one with a
This simple way avoid us to verify where a
3digit is found ( inside a
%X...block or elsewhere :-))
So, open the Replace dialog (
Ctrl + H)
ACTION Click on the
For instance, assuming the initial text below :
abc3.%X31 def abc3%X11223def abc3%X773777.3def abc3%X31 333def abc3% X321 333def abc3%X 321 333def abc333.%X333333.333def
It would be changed into :
abc4.%X31 def abc4%X11223def abc4%X773777.4def abc4%X31 444def abc4% X421 444def abc4%X 421 444def abc444.%X333333.444def
Just note that the
3digits are replaced by a
4digit , in the parts
%X 321, as they are not exactly of the form %Xdigitdigit…digit !!
(?-i)forces the search to be NON-insensitive to case ( so sensitive ! )
Then the regex engine looks for, either :
The exact string %X, followed by, at least, one digit (
Any digit 3, stored as group 1, because of the surrounding parentheses (
In replacement :
If group 1 exists ( second alternative ), we replace with a single
4digit ( part before the colon )
ELSE, we just rewrite the entire matched string %X##### ( regex
$0, after the colon )