Noob question: Insert text in bookmarked rows?

Micha
2013-06-17
2013-06-19
  • Micha

    Micha - 2013-06-17

    Hey,

    Couldn't find anything via google.
    Is it possible to insert text in rows that are bookmarked only?
    I'd also need the text at the end of every row, so if the text is "bla" I'd want it to be "bla xyz" etc.

    Thanks for your answer :)

     
  • THEVENOT Guy

    THEVENOT Guy - 2013-06-18

    Hello Micha,

    I don't think that N++ is able to insert (or delete ) some text in bookmarked lines !

    But, with the Menu command Search|Bookmark|Paste to ( Replace ) Bookmarked Lines, it's very easy to replace every bookmarked line with Clipboard contents.

    As for replacing lines, ending with the string bla, with the string blaxyz, a simple regex search/replacement can do the job, without any bookmark operation !

    SEARCH : bla$
    REPLACEMENT : $0xyz

    For this syntax, you need, at least, the 6.0 version of N++ and, of course, the regex search mode must have been set !

    Notes :

    • The symbol $ stands for the position end of line, before the EOL character(s) of each line

    • $0 represents the complete search string, so the string bla, in your example.

    Cheers,

    guy038

    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 :

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

    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 2013-06-18
  • Loreia2

    Loreia2 - 2013-06-18

    Hi Micha,

    if you created bookmarks manually, than you can't do what you ask directly in editor.
    Simplest way to do it would be to use RE expressions (as THEVENOT Guy already described in his usual impeccable way), or by writing a Python script that would dynamically check for presence of bookmark in every line, and then perform line replacement.

    BR
    Loreia

     
  • Micha

    Micha - 2013-06-18

    Hey,

    Thanks a lot so far!

    I'm sure the RE expressions will do the job, however I still have a problem.
    I would like to add text to every line without any brackets int it e.g:

    [K6][K5]
    AATT
    [A2]A2
    AA[KQ]
    AAJJ

    should be:

    [K6][K5]
    AATT XYZ
    [A2]A2
    AA[KQ]
    AAJJ XYZ

    I should have described it better, sorry. Thought there would be a way with bookmarks, but yea there is only the option to replace and this doesn't work for me :/.

     
    Last edit: Micha 2013-06-18
  • THEVENOT Guy

    THEVENOT Guy - 2013-06-18

    Hi, Micha, Loreia2,

    @ Loreia2

    Many thanks for the compliment ! I agree that I'm may look a bit perfectionist. But, to speak of regular expressions, I prefer to use good spelling and grammar. Moreover, some formatting text make reading more pleasant and allow you to point out some specific things. So I took some time to learn main Markdown syntax :)

    Like you, I think that, in many occasions, a simple python script would be better than a complicated regex. When I finish my French tutorial, about PCRE regular expressions in N++, and I hope an English version of it, I'll intend to learn this script language, very closed to N++.


    @ Micha

    I found three possible solutions, using regular expressions, to solve your search/replacement


    A)

    SEARCH : ^(?!.*\[).+
    REPLACEMENT : $&XYZ

    This search contains two parts :

    • a negative lookahead (?!.*\[) who searches, from current position, if there is NO open square bracket, further, on the current line.

    • the main search with ^ which represents the position beginning of line and .+ stands for all the characters of the non empty current line

    So, this regex matches the totality of the current line, ONLY IF, at the beginning of line, an open square bracket cannot be seen, further on the current line

    In replacement, $&, as $0, represents, as usual, the search string, so all the line, in our case.

    Note that the character [ is a META-character. So, it must be escaped \[


    B)

    SEARCH : ^((?!\[).)+$
    REPLACEMENT : $&XYZ

    This second regex searches for any standard character, ONLY IF this character is different of an open square bracket, at any position of the current line, due to the two anchors ^ and $

    Note that, with that construction, the negative lookahead is tested at each position of the current line.

    In the previous form, the negative lookahead is tested once only, at beginning of current line !


    C)

    SEARCH : ^(?(?!.*\[).+)$
    REPLACEMENT : $&XYZ

    This third form of regex uses a conditional expression, beginning by the two characters (?

    • the condition is the negative lookahead (?!.*\[)

    • the THEN part of the condition, .+, is located after the condition and before the closing parenthesis which ends the conditional expression

    • the ELSE part, which, normally, should follow a | character, after the THEN part, is absent, in our case.

    Thus the ELSE part is equivalent to the zero length string .{0}. But, as this regex is surrounded with the two anchors ^ and $, the ELSE part nevers occurs, because the line contains, at least, one open square bracket !

    To sum up, this third regex searches all the characters of non empty lines ONLY IF there's NO open square barcket seen, at the very beginning of the current line


    REMARKS :

    • If some lines of your text may contain the closing square bracket ONLY, all you have to do is to use an alternative and search for (\[|]) ( instead of \[ )

    Thus, the three regexes above, become :

    ^(?!.*(\[|])).+ ( case A )
    ^((?!(\[|])).)+$ ( case B )
    ^(?(?!.*(\[|])).+)$ ( case C )

    • If, in addition, you would like that EMPTY lines will be replaced by the string 'XYZ',
      just change the quantifier +, at the end of each regex, into the quantifier *

    I admit that these regexes use advanced features and that one needs some time to be acquainted with !!

    Cheers

    guy038

     
    Last edit: THEVENOT Guy 2013-06-19
  • Micha

    Micha - 2013-06-19

    Hey,

    Thanks a lot guy!
    Used A) and that worked very well.

    Cheers

    Micha

     
  • Loreia2

    Loreia2 - 2013-06-19

    THEVENOT Guy,

    I really love your posts, there is always something interesting in them, and I see them as a chance to learn something new. And the effort you put into using markdown is very appreciated !!

    About the Python.
    This is the language every computer professional/enthusiast should learn. It is really easy to learn it, and to use it, and it gives you tremendous power when you try to automate repetitious task.

    I picked it few years ago, almost by accident, after I gave up on Perl. And ever since Python has become dominant programming language in my tasks. In fact I invested a lot of effort in teaching my colleagues at work. I guess you should just come to Croatia to take my three day Python introduction course :-))

    Anyway, should you ever need help with Python, don't hesitate to contact me directly.
    udlv20 _ at _ googlemail _ dot _ com

    BR,
    Loreia

     
  • Loreia2

    Loreia2 - 2013-06-19

    What a coincidence, this popped on my G+ wall today:

    http://pythonbooks.revolunet.com/

    Check it out, there might be some intersting suff in there.

    BR,
    Loreia