Find and Replace a specific number but not when followed by a specific character
-
I need to replace a number in different strings.
eks. #ioStatusInput3.%X31
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 -
@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: \14If this doesn’t help you, i don’t know, try to be more specific and post more examples of strings you need to replace.
Kind regards!
-
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 a4digit
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)SEARCH
(?-i)%X\d+|(3)REPLACE
?{1}4:$0OPTIONS
Wrap aroundandRegular expressiontickedACTION Click on the
Replace Allbutton
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.333defIt would be changed into :
abc4.%X31 def abc4%X11223def abc4%X773777.4def abc4%X31 444def abc4% X421 444def abc4%X 421 444def abc444.%X333333.444defRemark :
Just note that the
3digits are replaced by a4digit , in the parts% X321and%X 321, as they are not exactly of the form %Xdigitdigit…digit !!
Notes :
-
The modifier
(?-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 (
%X\d+) -
Any digit 3, stored as group 1, because of the surrounding parentheses (
(3))
-
-
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 )
-
Best Regards,
guy038
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login