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: \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.

    Kind regards!



  • Hi, @Jesper-Bødewadt-Møller,

    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 3 digits elsewhere ( which are good candidates ), replace each one with a 4 digit

    This simple way avoid us to verify where a 3 digit is found ( inside a %X... block or elsewhere :-))


    So, open the Replace dialog ( Ctrl + H )

    SEARCH (?-i)%X\d+|(3)

    REPLACE ?{1}4:$0

    OPTIONS Wrap around and Regular expression ticked

    ACTION Click on the Replace All button


    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
    

    Remark :

    Just note that the 3 digits are replaced by a 4 digit , in the parts % X321 and %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 4 digit ( part before the colon )

      • ELSE, we just rewrite the entire matched string %X##### ( regex $0 , after the colon )

    Best Regards,

    guy038


Log in to reply