Add text at the end of line only i line starts with specific word? Regex



  • Hi
    i wonder how to add text at the end of the specific lines (only if a line starts with a specific word)
    like this:
    If a line starts with word DATA then add this text at the end of line “p:120 - 4 beats”

    example before:
    FILE "H:\WAV\database 352968 0 1022501958
    DATA t:untitled “example (98-01)”
    FILE "H:\WAV\database352968 0 1022502044
    DATA t:untitled “example (98-01)”
    FILE "H:\WAV\database
    DATA t:untitled “example (98-01)”

    and i want this:
    FILE "H:\WAV\database 352968 0 1022501958
    DATA t:untitled “example (98-01)” “p:120 - 4 beats”
    FILE "H:\WAV\database352968 0 1022502044
    DATA t:untitled “example (98-01)” “p:120 - 4 beats”
    FILE "H:\WAV\database
    DATA t:untitled “example (98-01)” “p:120 - 4 beats”

    is there a way to do it in selected text area maybe with regex?

    Thanks in advance



  • Hi, @chris-adamos,

    No problem with regexes, indeed ! So :

    • Get back to the very beginning of your file ( Ctrl + Origin )

    • Open the Replace dialog ( Ctrl + H )

    SEARCH (?-si)^DATA.+

    REPLACE $0 "p:120 - 4 beats" with a space character, right after $0

    • Select the regular expression search mode

    • Click, once, on the Replace All button OR several times on the Replace button

    Notes :

    • The first part, of the search, (?-is) are modifiers, which ensure you that :

      • The search will be performed, in a sensitive way ( = non-insensitive )

      • The dot special character means a single standard character, only ( not an End of Line char. )

    • Then, the ^DATA.+ part searches for the exact string DATA, at beginning of line, followed by the longest, non-empty range of any standard character

    • In replacement, the $0 syntax, just rewrites the entire searched string

    • Then the "p:120 - 4 beats" string, preceded with a space character, is simply added, to each matched line


    An other formulation could be :

    SEARCH (?-si)^DATA.+\K

    REPLACE \x20"p:120 - 4 beats"

    Notes :

    • The \K syntax, at the end of the regex, forces the regex engine to forget everything matched, before \K. So, it only matches the zero length string, at the end of each matched line

    • In replacement, the string \x20"p:120 - 4 beats" is, therefore, added, at the end of each matched line

    • Note that \x20 is, simply, the hexadecimal form of the space character

    • IMPORTANT : Due to the special \K syntax, you must use, exclusively, the Replace All button !

    Best Regards,

    guy038


Log in to reply