Help find 2nd occurrence and replace



  • Hello everyone I am trying to find the 2nd occurence of ; and replace with TTT but I am not able to come up with one. I tried to run the on below but it’s not working for me. My line is:

    Text;Text;HH:MM;Text;;;
    Text;Text;HH:MM;Text;;
    Text;Text;HH:MM;Text;;

    and I wanted to be

    Text;Text-TTT;HH:MM;Text;;;
    Text;Text-TTT;HH:MM;Text;;
    Text;Text-TTT;HH:MM;Text;;

    Here is what I have tried
    ^((.+?;){4})(.+)$
    \1-TTT\3



  • @Cesar-Silva said in Help find 2nd occurrence and replace:

    I am trying to find the 2nd occurence of ;

    Your regex got close I see. I edited it a bit and came up with the following rough solution. It works with your examples.
    Find What:^(.+?;.+?)(;.+)$
    Replace With:\1-TTT\2

    I used an online tester to get the solution as above. Try https://regex101.com/

    Cheers
    Terry



  • Hello, @cesar-silva, @terry-R and All,

    An alternate solution could be :

    SEARCH (?-s);.+?(?=;..:)

    REPLACE $0-TTT

    Assuming there is only 1 colon, in each row, at the third position of the third HH:MM field, of course !

    Best Regards

    guy038



  • @guy038 said in Help find 2nd occurrence and replace:

    An alternate solution could be

    Hi @guy038, All:

    Given that OP wants to get more an insertion than a replacement , wouldn’t be a ^ zero length match enough? I mean:

    Search: (?=;..:)
    Replace: -TTT

    Seems to work fine here.

    Best Regards



  • @astrosofista

    Don’t forget your leading (?-s) or detail a setting for the . matches newline checkbox. I find the former easier.

    All:

    I think maybe the OP attempted to “abstract” the data for us by using HH, MM, Text, etc…

    So perhaps an even better search term would be: (?=;\d\d:)

    “Better” as in : more restrictive to avoid changing unintended areas that just happen to match ;..:



  • Hi, @cesar-silva, @terry-R, @astrosofista, @alan-kilborn and All,

    @astrosofista

    Very clever, indeed. You’are right about it : this look-ahead, alone, is enough to, precisely determine the position of the zero-length string ;-))

    @alan-kilborn

    Your remarks are pertinent, too !. Finally, the best regex S/R, regarding the OP, should be :

    SEARCH (?-s)(?=;\d\d:)

    REPLACE -TTT

    At least 4 people needed to reach the pantheon of regular expressions, pheew ;-))

    Cheers,

    guy038



  • @guy038

    But now (?-s) is no longer needed. :-)

    For readers that don’t know why that is true: It is only needed if a . appears in the regular expression.



  • Hi, All,

    So, @Alan-kilborn has the last word !

    SEARCH (?=;\d\d:)

    REPLACE -TTT

    BR

    guy038



  • @Alan-Kilborn said in Help find 2nd occurrence and replace:

    Don’t forget your leading (?-s) or detail a setting for the . matches newline checkbox. I find the former easier.

    Oh, no, too much typewriting - it’s sunday morning :)

    Now, getting serious, you’re right on general cases, but in this particular one, given the data structure, that modifier has no effect at all. OP could check the . matches newline option but the output would be exactly the same.

    Of course, in other cases or situations it could do a lot of harm. Granted.



  • @astrosofista said in Help find 2nd occurrence and replace:

    but in this particular one, given the data structure

    Yep, given the data structure…

    …but what if what the OP has shown is only part of a huge file, with vastly different types of data in different sections of the file? The user does a Replace All, verifies the section that they are currently looking at (cool, looks good!), doesn’t notice the other sections, saves, quits, they a few days later notices all of the unintended replacements in other sections of the file. Too late!

    It’s better to be safer, rather than potentially sorrier. My general rule: If I use a . in a regex, I always specify (?s) or (?-s) ahead of it, and ignore the . matches newline box.

    Of course, it is problem in that posters with questions of these type rarely give enough information to give foolproof solutions.

    Also, I’ve noticed that you’re “casual” about the . matches newline or (?s) / (?-s) specifications in other thread’s postings, when it is NOT as clear cut as this thread’s is. Just a word to “be careful” – this setting matters a lot when it comes to regex.



  • Hi @Alan-Kilborn

    As far as I can understand, you take a more protective attitude towards OP than me, maybe experience had taught you that. It’s fine and have nothing against it. However, in my case and in the absence of any compelling reason I try to respond as accurately as possible to the data exemplified, assuming that the posted example is representative of the data set to be processed. In other words, I am confident that OP made a correct analysis of the issue.

    Which is the best approach? Neither is unreasonable a priori, nor are wrong those who decided to take a more or less intermediate position. I suppose context would help to make better decisions - see my second example, below.

    Concerning the casual point, what I understand - and if I am wrong, please correct me - is that the modifier (?s) or its negated form has real significance when it is quantified, i.e. affected by * or +. I mean, given this data:

    Line1
    Line2
    Line3
    Line4
    

    a search expression as Lin.\d will get four matches, no matter what adjustment has been made - (?s) or (?-s). In this case it is useless to take precautions. However, if a quantifier is added, either * or +, then the regex in one match could find the whole set given the . matches newline is checked.

    Thus, in the last regex I posted, https://community.notepad-plus-plus.org/post/52389, in which the data provided was insufficient, I took due precautions to control the regex, changing the modifier as appropriate so that the regex would do the desired job correctly.

    Anyway, rest assured that my posts aim to help people, as far as my limited knowledge allows. And if any response of mine is not entirely adequate, it will have been by unintentional error, but never by malice.



  • @astrosofista said in Help find 2nd occurrence and replace:

    I am confident that OP made a correct analysis of the issue.

    I guess you haven’t read as many of these types of postings as I have.
    You’re predicating your subsequent statements on that “correct analysis”. :-)

    Sure the case of . by itself (not in a .*, .*?, .+ or .+? construct, and some others less frequently used) is fairly immune to trouble; for Windows files (which is what we talk about mostly, obviously) a single . shouldn’t be able to cause line-ending harm (it would take two . to match the \r\n).

    It’s okay, I was just trying to offer some friendly advice concerning .. I think it has been beat to death.

    rest assured that my posts aim to help people,

    Of course. I think you’ve been a strong contributor to date.



  • Hi, @astrosofista, @alan-kilborn and All,

    Yes, at first sight, we could think that a single regex dot character, between two characters, is handled in a same way, whatever the state of the s in-line modifier and/or the state of the . matches newline option

    But it’s not always the case :-( For instance, let’s suppose the 3-lines text, below, with Unix EOL, the last line without any EOL, and a space char between the last two c letters

    cbcc1cc LF
    ccZcc9cc LF
    cc c

    The regex (?s)c.c would match, successively, the 7 strings cbc , c1c , cLFc , cZc , c9c , cLFc and c c, so all the text !

    Whereas the regex (?-s)c.c would match the 5 strings cbc , c1c , cZc , c9c and c c, only !


    Therefore, it is best to remain precise and indicate the modifiers (?s) and/or (?-s), whenever necessary ;-)

    Best Regards,

    guy038



  • @guy038 said in Help find 2nd occurrence and replace:

    But it’s not always the case :-( For instance, let’s suppose … Unix EOL

    Which is exactly why I had said:

    for Windows files

    :-)



  • @Alan-Kilborn said in Help find 2nd occurrence and replace:

    It’s okay, I was just trying to offer some friendly advice concerning …

    And I took it like this. What’s more, it’s even good advice, since I only considered the case of Windows, ignoring Unix and Mac.

    Thank you, and I’ll keep that in mind.

    Have fun!



  • Hi @guy038

    Thank you, your’s is good advise, as well as @Alan-Kilborn’s. As I told him, I only took into consideration the Windows case, ignoring other platforms.

    Will try to not forget it.

    Best Regards


Log in to reply