Replace all instances of specific character only in one portion of the line

  • spoko

    I need to replace all tabs, but only between two specific words. The number of tabs doesn't remain constant, and I want to put this into a macro, so I need to be able to do it with a single S/R. Is this even possible?

    Specifically, I have a list of book entries in this format:
    (I've replaced tabs with \t here so you can see the formatting.)

    Title:\tBloggerati, Twitterati: How Blogs and Twitter Are Transforming Popular Culture\tAuthors:\tCross, Mary\tPublication Information:\tBlahBlahBlah\tRelated ISBNs:\t9780313384844. 9780313384851.\tAccession Number:\t369626\tDatabase:\teBook Collection (EBSCOhost)
    Title:\tE-personal Finance : Managing Your Money and Investments Online\tAuthors:\tDolan, Ken\tDolan, Daria\tBrown, Betsy\tSnyder, Jan\tPublication Information:\tBlahBlahBlah\tRelated ISBNs:\t9780672318795. 9780585284163.\tAccession Number:\t38439\tDatabase:\teBook Collection (EBSCOhost)

    The problem is with entries like the second one, where there are multiple authors. I want to consolidate that part so it looks more like
    Authors:\tDolan, Ken/Dolan, Daria/Brown, Betsy/Snyder, Jan
    Or better still
    Authors:\tKen Dolan, Daria Dolan, Betsy Brown, Jan Snyder
    though I can live with the first one for now.

    But since tabs are also used to separate 'columns' within each listing, I can't just do a simple S/R. And since the number of authors might range anywhere from one to . . . I don't know, eight or nine at least, I can't just hard-code a search that looks for a certain number of tabs between 'Author:' and 'Publication Information:'.

    So, is there a way to replace all tabs, but only within that one portion of each line?


    Hello Spoko,

    If all your author's designation, in your file, are of the form : "\tName, First name\t", your second wish can be easily achieve with two search/replacement, in regular expression mode :-)

    First S/R ( with search mode = Regular expression )

    SEARCH : \t([\w -]+), ([\w -]+)(?=\t) with two spaces between \w and the minus symbol and a space after the comma

    REPLACE : , \2 \1 with two spaces after the comma and after the digit 2

    Click on the Replace All button

    Second S/R ( with search mode = Regular expression )

    SEARCH : Authors:,

    REPLACE : Authors:\t

    This very simple S/R allow to change the comma, due to the first replacement, in each line, of the previous S/R, into a tabulation character

    Notes, concerning the first S/R

    • the search expression looks for \tName, First name, ONLY if it's followed with a tabulation As the last \t is inside a lookaround, it doesn't belong, of course, to the search but must be matched to satisfy the complete regex ! This form allows names and/or first names separated with space(s) and/or dashe(s) as, for example, in French, : Marie-Noelle DE LARUE .

    It also creates two groups : \1 which represents the name of the author and \2 which represents the first name.

    • the replace expression re-write ", First name Name", according to the contains of the groups \1 and \2, with a comma, at the beginning, to separate the different authors of the same book.



    P.S. :

    You will find good documentation, about the new Perl Common Regular Expressions (PCRE), used by N++, since the 6.0 version, at the TWO addresses below :

    The FIRST link concerns the syntax of regular expressions in SEARCH

    The SECOND link concerns the syntax of regular expressions in REPLACEMENT

    Last edit: THEVENOT Guy 2014-01-16
  • spoko

    Thank you! I'm self-taught with regexes, and can do most things I need to do. But somewhere along the line, I missed that lookaround feature. I'll be using that; it's pretty sweet. I'll also be spending some time on the documentation pages you linked to, so thanks for those.