Loading ...
Sorry, an error occurred while loading the content.
 

Search backwards

Expand Messages
  • abairheart
    Hi, Couple of years ago, it was dtermined, that NTP cannot search backwards with regular expressions. This topic is still not addressed in the help file, that
    Message 1 of 8 , Jun 2, 2007
      Hi,

      Couple of years ago, it was dtermined, that NTP cannot search
      backwards with regular expressions. This topic is still not addressed
      in the help file, that I can see, but has it changed?

      For example, I want to select a closing xml tag, say </STUFF>, and
      find its matching starting tag. Since multiple such tags could exist,
      it is not procatical to search from start of document.

      The command

      ^!Find (</^%tag%>)|(<^%tag%) BRIS

      always searches forward.


      Has anyone got any ideas?



      Abair
    • Sheri
      ... With careful use of search in highlight and regex greediness you can accomplish the same thing. But if using NoteTab Pro, you do have to avoid creating a
      Message 2 of 8 , Jun 3, 2007
        --- In ntb-clips@yahoogroups.com, "abairheart" <abairheart@...> wrote:
        >
        > Hi,
        >
        > Couple of years ago, it was dtermined, that NTP cannot search
        > backwards with regular expressions. This topic is still not
        > addressed in the help file, that I can see, but has it changed?
        >
        > For example, I want to select a closing xml tag, say </STUFF>,
        > and find its matching starting tag. Since multiple such tags
        > could exist, it is not procatical to search from start of
        > document.
        >
        > The command
        >
        > ^!Find (</^%tag%>)|(<^%tag%) BRIS
        >
        > always searches forward.
        >
        >
        > Has anyone got any ideas?

        With careful use of search in highlight and regex greediness you can
        accomplish the same thing. But if using NoteTab Pro, you do have to
        avoid creating a regular expression that begin with a caret when
        searching in a highlight, because when finding or replacing such a
        pattern within highlight text, NTP clobbers the highlight if the
        highlight doesn't begin at the start of line. I reported it as a bug,
        but the author considers it a feature.

        To use the following clip, put the cursor inside the tag to be
        queried. It searches "backwards" if its a closing tag and forward if
        its an starting tag. It finds the closest tag of the same type, so
        depending on the document, it might find a tag of the same type which
        is not matched as far as opening vs closing. That seemed to be what
        you were trying to do but I could be wrong about that.

        Regards,
        Sheri

        H="Locate fellow tag"
        ;2007-06-03 Created by Sheri Pierce
        ;requires NoteTab version 5.1+
        ^!Set %userstart%=^$GetRowStart$:^$GetColStart$
        ^!Set %userend%=^$GetRowEnd$:^$GetColEnd$
        ^!Set %userscr%=^$GetRowTop$
        ^!SetScreenUpdate Off
        ^!Select HtmlTag
        ^!IfError Next Else Skip_3
        ^!MoveCursor +1
        ^!Select HtmlTag
        ^!IfError Errmsg
        ^!Set %ss%=^$GetSelSize$
        ^!Setcursor ^$GetRow$:^$GetColStart$
        ^!MoveCursor +1
        ^!Select word
        ^!IfError Next Else Skip_3
        ^!MoveCursor +1
        ^!Select word
        ^!IfError ErrMsg5
        ^!Set %tag%=^$GetSelection$
        ^!Select HtmlTag
        ^!Set %begin%=^$GetRow$:^$GetColStart$
        ^!Set %endtag%=^$GetRow$:^$GetColEnd$
        ^!IfMatch "\A\<\/.+\>" "^$GetSelection$" Backward Else Forward
        :Backward
        ^!SetCursor ^%begin%
        ^!SelectTo 1:1
        ^!Find "(?s).*(</?^%tag%.*?>)" R1HS
        ^!IfError Errmsg3
        ^!Jump Select_Start
        ^!Selectto ^%endtag%
        ^!SetView ^%userscr%:1
        ^!Goto end
        :Forward
        ^!SetCursor ^%endtag%
        ^!Jump Text_End
        ^!Selectto ^%endtag%
        ^!Find "(?s).*?(</?^%tag%.*?>)" R1HS
        ^!IfError Errmsg4
        ^!Jump Select_End
        ^!selectto ^%begin%
        ^!SetView ^%userscr%:1
        ^!Goto end
        :Errmsg
        ^!Prompt no tag here
        ^!Goto end
        :Errmsg2
        ^!Prompt not started on a tag
        ^!Goto end
        :Errmsg3
        ^!SetCursor ^%userstart%
        ^!Selectto ^%userend%
        ^!Prompt No other ^%tag% tags found prior to starting tag
        ^!Goto end
        :Errmsg4
        ^!SetCursor ^%userstart%
        ^!Selectto ^%userend%
        ^!Prompt No other ^%tag% tags found after starting tag
        ^!Goto end
        :Errmsg5
        ^!SetCursor ^%userstart%
        ^!Selectto ^%userend%
        ^!Prompt No tag id located
        ^!Goto end
        ;end of clip
      • abairheart
        Hi Sheri, Thanks for your reply. I ll give it whirl. ... Yes I am trying to match a specific pair of tags. But I have that part working in a forward searching
        Message 3 of 8 , Jun 3, 2007
          Hi Sheri,


          Thanks for your reply. I'll give it whirl.


          --- In ntb-clips@yahoogroups.com, "Sheri" <silvermoonwoman@...> wrote:
          >
          > To use the following clip, put the cursor inside the tag to be
          > queried. It searches "backwards" if its a closing tag and forward if
          > its an starting tag. It finds the closest tag of the same type, so
          > depending on the document, it might find a tag of the same type which
          > is not matched as far as opening vs closing. That seemed to be what
          > you were trying to do but I could be wrong about that.
          >
          > Regards,
          > Sheri


          Yes I am trying to match a specific pair of tags. But I have that part
          working in a forward searching clip. Perhpas I can incorporate your
          clip, or technique. I'll post the finished clip, if it works.


          Abair
        • abairheart
          Hi Sheri, Thanks for your help. ... I m not sure how it does that. MONO-spaced: ^!Find (?s).*( ) R1HS ... and ^!Find (?s).*?( )
          Message 4 of 8 , Jun 4, 2007
            Hi Sheri,

            Thanks for your help.


            > It finds the closest tag of the same type,

            I'm not sure how it does that.

            MONO-spaced:

            ^!Find "(?s).*(</?^%tag%.*?>)" R1HS
            ----------^---------------------^
            and

            ^!Find "(?s).*?(</?^%tag%.*?>)" R1HS
            --------------^

            Doesn't '(?s)' just gobble up everything to the last match, including
            previous matches?
            And then the '1' in the find options restricts the search to the first
            match in the selection, rather than the last?

            I've seen it find the right tag to a pair that has no embedded nests
            of the same type of tag, but I do not understand how it does that.
            Any idea, what I'm missing here?

            Finally, in the forward search, what does the '?' (the only difference
            between the two find commands) before the ^%tag% pattern do?


            Thanks very much,


            Abair.
          • Sheri
            ... In the pattern above, the .* consumes all the characters matching dot (greedy), til the end of the string, then the regex engine backtracks until a
            Message 5 of 8 , Jun 4, 2007
              --- In ntb-clips@yahoogroups.com, "abairheart" <abairheart@...> wrote:
              >
              > > It finds the closest tag of the same type,
              >
              > I'm not sure how it does that.
              >
              > MONO-spaced:
              >
              > ^!Find "(?s).*(</?^%tag%.*?>)" R1HS
              > ----------^---------------------^

              In the pattern above, the .* consumes all the characters matching dot
              (greedy), til the end of the string, then the regex engine backtracks
              until a matching tag in angle brackets exists at the end. Because of
              the (?s), dot matches all characters including line breaks. The one in
              options just causes the text matching the subpattern rather than the
              entire pattern to get selected by the ^!Find. To locate only opening
              tags, you could remove /? from inside the angle brackets.

              > and
              >
              > ^!Find "(?s).*?(</?^%tag%.*?>)" R1HS
              > --------------^

              In this pattern there is no backtracking because .*? is not greedy.
              It is searching forward from the starting point until it finds the
              matching tag. Again the 1 in options causes the ^!Find to select the
              1st subpattern rather than the whole match. To locate only a closing
              type tag, you should remove the question mark after the / inside the
              angle brackets.

              >
              > Doesn't '(?s)' just gobble up everything to the last match,
              > including previous matches?

              (?s) doesn't gobble anything. It just causes line breaks to be among
              the types of characters that match dot. However, without (?s), neither
              .* nor .*? will gobble as far, because it will stop at the first line
              break.

              > And then the '1' in the find options
              > restricts the search to the first match in the selection, rather
              > than the last?

              The 1 relates to the 1st subpattern in the regular expression; it has
              nothing to do with the number of matches in the search field. Its a
              component of every match. There is only one subpattern, see the
              parentheses around the tag. The parentheses around (?s) don't count.

              Regards,
              Sheri
            • abairheart
              Thanks very much, that cleared up a couple of things. ... That s what I meant by gobbling everything (including newlines). ... Ahhh, I understand. The ? has
              Message 6 of 8 , Jun 4, 2007
                Thanks very much, that cleared up a couple of things.

                > until a matching tag in angle brackets exists at the end. Because of
                > the (?s), dot matches all characters including line breaks.

                That's what I meant by gobbling everything (including newlines).


                > In this pattern there is no backtracking because .*? is not greedy.

                Ahhh, I understand. The '?' has so many uses now, that I lose track.


                >
                > The 1 relates to the 1st subpattern in the regular expression; it has
                > nothing to do with the number of matches in the search field. Its a
                > component of every match.

                OK, I shouldda know that.



                Thanks again,


                Abair
              • normfsime
                ... bug, ... which ... I have a workaround which I developed but is not quite ready for prime time yet. It is cumbersome, but likely to prove useful with
                Message 7 of 8 , Mar 22, 2008
                  --- In ntb-clips@yahoogroups.com, "Sheri" <silvermoonwoman@...> wrote:
                  >
                  > --- In ntb-clips@yahoogroups.com, "abairheart" <abairheart@> wrote:
                  > >
                  > > Hi,
                  > >
                  > > Couple of years ago, it was dtermined, that NTP cannot search
                  > > backwards with regular expressions. This topic is still not
                  > > addressed in the help file, that I can see, but has it changed?
                  > >
                  > > For example, I want to select a closing xml tag, say </STUFF>,
                  > > and find its matching starting tag. Since multiple such tags
                  > > could exist, it is not procatical to search from start of
                  > > document.
                  > >
                  > > The command
                  > >
                  > > ^!Find (</^%tag%>)|(<^%tag%) BRIS
                  > >
                  > > always searches forward.
                  > >
                  > >
                  > > Has anyone got any ideas?
                  >
                  > With careful use of search in highlight and regex greediness you can
                  > accomplish the same thing. But if using NoteTab Pro, you do have to
                  > avoid creating a regular expression that begin with a caret when
                  > searching in a highlight, because when finding or replacing such a
                  > pattern within highlight text, NTP clobbers the highlight if the
                  > highlight doesn't begin at the start of line. I reported it as a
                  bug,
                  > but the author considers it a feature.
                  >
                  > To use the following clip, put the cursor inside the tag to be
                  > queried. It searches "backwards" if its a closing tag and forward if
                  > its an starting tag. It finds the closest tag of the same type, so
                  > depending on the document, it might find a tag of the same type
                  which
                  > is not matched as far as opening vs closing. That seemed to be what
                  > you were trying to do but I could be wrong about that.
                  >
                  > Regards,
                  > Sheri
                  >
                  > H="Locate fellow tag"
                  > ;2007-06-03 Created by Sheri Pierce
                  > ;requires NoteTab version 5.1+
                  > ^!Set %userstart%=^$GetRowStart$:^$GetColStart$
                  > ^!Set %userend%=^$GetRowEnd$:^$GetColEnd$
                  > ^!Set %userscr%=^$GetRowTop$
                  > ^!SetScreenUpdate Off
                  > ^!Select HtmlTag
                  > ^!IfError Next Else Skip_3
                  > ^!MoveCursor +1
                  > ^!Select HtmlTag
                  > ^!IfError Errmsg
                  > ^!Set %ss%=^$GetSelSize$
                  > ^!Setcursor ^$GetRow$:^$GetColStart$
                  > ^!MoveCursor +1
                  > ^!Select word
                  > ^!IfError Next Else Skip_3
                  > ^!MoveCursor +1
                  > ^!Select word
                  > ^!IfError ErrMsg5
                  > ^!Set %tag%=^$GetSelection$
                  > ^!Select HtmlTag
                  > ^!Set %begin%=^$GetRow$:^$GetColStart$
                  > ^!Set %endtag%=^$GetRow$:^$GetColEnd$
                  > ^!IfMatch "\A\<\/.+\>" "^$GetSelection$" Backward Else Forward
                  > :Backward
                  > ^!SetCursor ^%begin%
                  > ^!SelectTo 1:1
                  > ^!Find "(?s).*(</?^%tag%.*?>)" R1HS
                  > ^!IfError Errmsg3
                  > ^!Jump Select_Start
                  > ^!Selectto ^%endtag%
                  > ^!SetView ^%userscr%:1
                  > ^!Goto end
                  > :Forward
                  > ^!SetCursor ^%endtag%
                  > ^!Jump Text_End
                  > ^!Selectto ^%endtag%
                  > ^!Find "(?s).*?(</?^%tag%.*?>)" R1HS
                  > ^!IfError Errmsg4
                  > ^!Jump Select_End
                  > ^!selectto ^%begin%
                  > ^!SetView ^%userscr%:1
                  > ^!Goto end
                  > :Errmsg
                  > ^!Prompt no tag here
                  > ^!Goto end
                  > :Errmsg2
                  > ^!Prompt not started on a tag
                  > ^!Goto end
                  > :Errmsg3
                  > ^!SetCursor ^%userstart%
                  > ^!Selectto ^%userend%
                  > ^!Prompt No other ^%tag% tags found prior to starting tag
                  > ^!Goto end
                  > :Errmsg4
                  > ^!SetCursor ^%userstart%
                  > ^!Selectto ^%userend%
                  > ^!Prompt No other ^%tag% tags found after starting tag
                  > ^!Goto end
                  > :Errmsg5
                  > ^!SetCursor ^%userstart%
                  > ^!Selectto ^%userend%
                  > ^!Prompt No tag id located
                  > ^!Goto end
                  > ;end of clip
                  >

                  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

                  I have a workaround which I developed but is not quite ready for
                  prime time yet. It is cumbersome, but likely to prove useful with
                  large documents.

                  1. Establish the location of the current Entry.

                  2. Search backward with the best NTBP non-regex approximation.

                  3. MoveCursor -3

                  4. Now search forward with your regex pattern.

                  5. At each hit, record the hit location, then check that you have not
                  passed the Entry location.

                  6. When you have passed your Entry location, the last hit location is
                  the location you want (i.e., the FIRST backward match of the pattern
                  you want).

                  P.S. Pretty cumbersome, Ce n'est pas?
                • Don - HtmlFixIt.com
                  I have find matching tag clips. Mine are not regex. It searches forwards from an opening tag and backwards from a closing tag. Is that what is being
                  Message 8 of 8 , Mar 22, 2008
                    I have find matching tag clips. Mine are not regex. It searches
                    forwards from an opening tag and backwards from a closing tag. Is that
                    what is being discussed?
                  Your message has been successfully submitted and would be delivered to recipients shortly.