Mass replace text



  • Hello everyone!
    Is it possible text like this

    =R3+R7+R18+R25+R36+R45+R75+R51+R69C4T1

    edit to text like this

    =T1R3C4+T1R7C4+T1R18C4C4+T1R25C4C4+T1R36C4+T1R45C4+T1R75C4+T1R51C4+T1R69C4

    via regex?
    Digits and Operators may be different.



  • When asking for regex help, other than showing what you tried and why it didn’t work, it’s always good to explain your rule in plain language, and to give examples of text that should change, and text that shouldn’t.

    Your example seems to contradict itself:

    • the R3 and R7 seem to get surrounded by T1 before and C4 after
    • the R18 and R25 seem to get surrounded by T1 before and C4C4 after
    • the R36, R45, R75, and R51 seem to get surrounded by T1 before and C4 after
    • the R69C4T1 seems to strip the C4T1, then get surrounded by T1 and C4.

    I cannot make a consistent rule in my head (let alone come up with a regex for it) given those strange circumstances.

    My best guess is that the double-C4 for R18 and R25 was an accident, and that you want a rule something like “using = or + to separate words, strip all but R followed by one or more digits from the word, then surround what remains by T1 before and C4 after”. If this is the rule you are looking for, @guy038 will probably have it implemented seconds after I hit submit. If not, please do a better job of explaining what you really want.



  • So here’s one attempt at my rule:

    Given

    =R3+R7+R18+R25+R36+R45+R75+R51+R69C4T1
    

    Find (?<=[+=])\w*(R\d+)\w*(?=([+]|\R|$))
    Replace with T1$1C4
    Search mode = Regular Expression

    Results in

    =T1R3C4+T1R7C4+T1R18C4+T1R25C4+T1R36C4+T1R45C4+T1R75C4+T1R51C4+T1R69C4
    

    As I said, this isn’t quite what you showed, but what you showed didn’t seem consistent to me.

    Anyway, if this doesn’t work for you, let us know in more detail what the real rule is.



  • Hello, @ариан-назари, and All,

    I suppose that your exact need is :

    BEFORE :    =R3+R7+R18+R25+R36+R45+R75+R51+R69
    
    AFTER       =T1R3C4+T1R7C4+T1R18C4+T1R25C4+T1R36C4+T1R45C4+T1R75C4+T1R51C4+T1R69C4
    

    Here is a first regex which replaces any word xxxxx, located after an eqal sign =, with T1xxxxxC4

    SEARCH (?-s)(\G|=).*?\K\w+

    REPLACE T1$0C4

    But I prefer this second regex, more selective, which replaces any word xxxxx, after an equal sign =, with possible blank chars, right after the = sign, and, with at most, one blank char between subsequent chars/word and a next word, of the current line

    SEARCH (?-s)(\G|=\h*)((?!\h{2,}).)*?\K\w+

    REPLACE T1$0C4

    So, let’s suppose the test file , below :

        =R3+R7+R18+R25+R36+R45+R75+R51+R69
    	
    bla bla blah
    
    =R3 +R7- R18 +R25 -   R36 +  R45-R75 +  R51 -  R69
    
       =R3+R7 + R18+R25   This is the first test    = R36+R45  +  R75 + R51+R69
    
    bla bla bla blah
    
       = R3+R7 + R18+R25 This is a test = R36+R45 + R75 + R51+R69
    
       =   R3+R7 +   R18+R25   This is second test    = R36+R45 + R75 + R51+R69
    
    =R3+R7+R18+R25+R36+R45+R75+R51+R69
    
    This is the third test       =       R3+R7+R18+R25 + R36 + R45+R75+R51+R69
    
    Final test       = R3+R7+R18+R25 + R36 + R45+R75+R51+R69
    

    Then, after performing the second regex S/R, you should obtain :

        =T1R3C4+T1R7C4+T1R18C4+T1R25C4+T1R36C4+T1R45C4+T1R75C4+T1R51C4+T1R69C4
    	
    bla bla blah
    
    =T1R3C4 +T1R7C4- T1R18C4 +T1R25C4 -   R36 +  R45-R75 +  R51 -  R69
    
       =T1R3C4+T1R7C4 + T1R18C4+T1R25C4   This is the first test    = T1R36C4+T1R45C4  +  R75 + R51+R69
    
    bla bla bla blah
    
       = T1R3C4+T1R7C4 + T1R18C4+T1R25C4 T1ThisC4 T1isC4 T1aC4 T1testC4 = T1R36C4+T1R45C4 + T1R75C4 + T1R51C4+T1R69C4
    
       =   T1R3C4+T1R7C4 +   R18+R25   This is second test    = T1R36C4+T1R45C4 + T1R75C4 + T1R51C4+T1R69C4
    
    =T1R3C4+T1R7C4+T1R18C4+T1R25C4+T1R36C4+T1R45C4+T1R75C4+T1R51C4+T1R69C4
    
    This is the third test       =       T1R3C4+T1R7C4+T1R18C4+T1R25C4 + T1R36C4 + T1R45C4+T1R75C4+T1R51C4+T1R69C4
    
    Final test       = T1R3C4+T1R7C4+T1R18C4+T1R25C4 + T1R36C4 + T1R45C4+T1R75C4+T1R51C4+T1R69C4
    

    Notes :

    • First the (?-s) modifier means that any dot represents a single standard char ( not an EOL one )

    • Then, (\G|=\h*) part matches the end of a previous match, \G, or an equal sign = followed by a possible range of blank characters, =\h*

    • After, the ((?!\h{2,}).)*? part looks for the shortest range of characters, which does not contain, two or more consecutive blank chars

    • And this until the regex \K\w+ which either :

      • Resets the regex engineworking position and forgets any previous match

      • Match the next word found

    • In replacement, the overall match, $0 ( the word ) is simply, surrounded :

      • On the left, with the T1 string

      • On the right, with the C4 string


    Of course, if you need more restrictive rules and/or if your text is fairly structured, we could adjust this regex

    Best Regards,

    guy038



  • Thank you for your help and support! @Peter-Jones and @guy038
    I’m not good enough in regular expressions.
    I’m trying convert (R\d*) to (T\d*)(R\d*)(C\d*) format using capture groups in row.
    My best is:
    Find: (R\d*)(.*?)?(C\d*)(T\d*)

    Replace:?1\4\1\3\2

    But it work only for the first match.

    Also I’ll try with alternation like:
    Find:(R\d*)|(C\d*)(T\d*)
    Replace:?1\3\1\2
    It doesn’t work too.



  • Is it possible in Replace edit capture group and reinsert another captured groups like this?1$3$1$2? Because “T” and “C” may have another digits.


Log in to reply