Find the last occurence before a condition



  • Hi,
    I’m a new user of notepad++ regex so I don’t know the most advanced feature.
    My code is something like( from a gcode of a cnc machine):

    T11 M06
    (bunch of lines)
    T3 M06
    (lines)
    Turn
    (lines)
    T7 M06
    (lines)

    I need either to have the cursor at the T3 M06 before the word Turn or to have the search which includes from the T3 M06 until Turn.
    All the number after the letter T are random and so the number of times the word Turn is written, it couldn’t be at all; I need to be in regex so the code I need to apply can be used everytime
    the word Turn is used.

    Sorry about any grammatical mistake.
    Thanks in advance



  • Hello Mario,

    Given the possible example text, below :

    T11 M06
    Line 2
    Line 3
    Line 4
    T3 M06
    Line 6
    Line 7
    Line 8
    Turn
    Line 10
    Line 11
    Line 12
    Line 13
    Turn
    Line 15
    Line 16
    Line 17
    T7 M06
    Line 19
    Line 20
    Line 21
    

    Now let’s suppose that :

    • The cursor is located before the upper letter T of the header T11 M06, at beginning of the text. Do you want to :

      • Select the range, from the header T11 M06, till Line 4, included, before the next header T3 M06
      • Select the range, from the header T11 M06, till the first string Turn, at line 9
      • Select the range, from the header T11 M06, till the last string Turn, at line 14
      • Select the range, from the header T11 M06, till the last line 21
    • The cursor is located before the upper letter T of the header T3 M06. Do you want to :

      • Select the range, from the header T3 M06, till the first string Turn, at line 9
      • Select the range, from the header T3 M06, till the last string Turn, at line 14
      • Select the range, from the header T3 M06, till Line 17, before the next header T7 M06
      • Select the range, from the header T3 M06, till the last line 21
    • The cursor is located before the upper letter T of the header T7 M06. I suppose that you want to select the range from the header T7 M06 till the last line 21 of the file, despite of the absence of an other command Turn, wouldn’t you ?

    See you later, for your additional information ! Be sure that there’s (almost) always a solution when dealing with regular expressions :-))

    Best Regards,

    guy038

    P.S. :

    Two more questions :

    • Is the command Turn should be included or excluded, from the selected range ?

    • May be, would you prefer that the search process places the cursor on specific locations, in your text ?



  • Sorry if my post was confusing.
    I wanted at the end a way to select only the letter T, followed by the number, but only the one before the word turn and not the others.
    I write a post here because once I’ll find a way to find that letter I’m using another regex code;
    using the normal search is not ideal becuase I wanted my code to be useful every time the word turn is written.

    I hope it’s explained it better now.



  • Hi Mario,

    OK, I think I got it !

    Given your original text, below :

    T11 M06
    (bunch of lines)
    T3 M06
    (lines)
    Turn
    (lines)
    T7 M06
    (lines)
    

    You would like to search, ONLY, for the range :

    T3 M06
    (lines)

    because it’s the only range, which is followed by the word Turn ?

    Just tell me if I’m right about it !

    In that case, do you want :

    • The cursor located just before the string T3
    • The string T3 selected, only
    • The string T3 M06 selected, only
    • The bunch of lines between T3 M06 and Turn selected, exclusively
    • The bunch of lines T3 M06Turn

    Cheers,

    guy038



  • Hi thank you for your answer.
    Anything than can move the cursor on the letter t3 or select it.
    Just to clarify the word turn can be more than once and every time I want the letter T+numer just before that; the bunch of lines are mostly the same and can’t be used for selection because there’s no way to distinguish those in first section or in the last.

    Thanks in advance.



  • Hi Mario,

    Sorry for my late answer, but I was just eating a bit !

    I look quite “slow” to understand exactly what you need, but we’ll get the right solution, in the end :-)

    So, could it be that you would change the range of lines :

    T3 M06
    (lines)
    Turn
    

    into the range A :

    T3 M06
    (lines)
    T3
    Turn
    

    or, may be, into the range B :

    T3 M06
    (lines)
    T3 M06
    Turn
    

    See you later,

    Cheers,

    guy038



  • I’m definitely bad at explain myself.
    I’ll explain my problem in another way:
    I have this code:
    t
    t
    t
    s
    t
    s
    t
    t
    t
    s
    I want to select ,once at a time, only the t before s with the search regex.



  • Hi, Mario,

    Waouuuuuuh ! This time it’s the good one :-)

    From your last example, I would say :

    • Place your cursor, before the part of text to scan

    • Open the Find dialog ( CTRL + F )

    • Select the Regular expression search mode

    • Write, in the Find what zone, the regex t(?=\Rs)

    • Finally, click, several times of the Find Next button

    Et voilà !

    This regex will select any letter t, located at the end of a line, ONLY IF it is followed by a letter s, at the beginning of the next line :-)

    Note :

    • The syntax \R stands for any End of Line characters ( \r\n in Windows files, \n in Unix/OSX files or \r in old MAC files )

    • The structure (?=....) is called a look-around and more exactly, a condition called a positive look-ahead. The string written inside ( \Rs ) must be present, after the letter t, for the regex engine detects a positive match, although it is NEVER part of the final match, which is, only, the letter t

    Remark :

    Generally speaking, the global form (regex A|string A|character A)(?=(regex B|string B|character B)) tries to match regex A or string A or character A, ONLY IF it’s immediately followed by, either, the regex B, or string B or character B !


    Feel free to ask me for a narrowed solution !

    Cheers,

    guy038

    P.S. :

    You’ll find good documentation, about the new Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by Notepad++, since its 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 explains the syntax, of regular expressions, in the SEARCH part

    • The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part

    We may, also, look for valuable information, on the sites, below :

    http://www.regular-expressions.info

    http://www.rexegg.com

    http://perldoc.perl.org/perlre.html

    To end with, you may ask, the N++ Community, for infos on any tricky regex that you came across OR for building any tricky regex, for a particular purpose :-))



  • Thank you for your time and your answers.
    I’ll try to make it work; I don’t know how to mark this topic as solved by the way.


Log in to reply