Menu

#2157 Regex bug for replace all '\<' with a word

Bug
closed-fixed
5
2023-12-25
2020-02-21
Zufu Liu
No

Regex replace all \< with a word (add prefix for all word) not works as expected:
Open SciTE type the word "hello", after replace all with "With", "hello" become "WithhWitheWithlWithlWitho".

however add suffix for all word by replace \> works.

Related

Bugs: #2405

Discussion

  • Zufu Liu

    Zufu Liu - 2020-02-21

    Another bug: ^\< can find words at line start, but \> or \>$ can't find words at line end.

     
    • Zufu Liu

      Zufu Liu - 2021-09-20

      The search bug was fixed, maybe in 5.1.1 with 8873 (80d3b0c2b86a) Extract word edge detection to prepare for fixing bug..

       
      • Zufu Liu

        Zufu Liu - 2021-09-21

        Sorry, not yet fixed. however, \w\> or \w\>$ does find the last letter.

         
  • Neil Hodgson

    Neil Hodgson - 2020-02-22
    • labels: regex --> regex, replace
    • status: open --> open-accepted
     
  • Neil Hodgson

    Neil Hodgson - 2021-09-20
    • labels: regex, replace --> regex, replace, scintilla
    • summary: Regex bug for replace all '/<' with a word --> Regex bug for replace all '\<' with a word
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,4 @@
    -Regex replace all `/&lt;` with a word (add prefix for all word) not works as expected:
    +Regex replace all `\&lt;` with a word (add prefix for all word) not works as expected:
     Open SciTE type the word &#34;hello&#34;, after replace all with &#34;With&#34;, &#34;hello&#34; become &#34;WithhWitheWithlWithlWitho&#34;.
    
     however add suffix for all word by replace `\&gt;` works.
    
     
  • Neil Hodgson

    Neil Hodgson - 2021-09-20

    A problem here is that RESearch::Execute treats the start of search as a line start and thus word start. It should probably be passed the line start value as a parameter or be able to discover it.

    I have changed the title and description from /< to \< as it didn't make sense otherwise. please advise if /< was meant.

     
    👍
    1

    Last edit: Neil Hodgson 2021-09-20
  • Zufu Liu

    Zufu Liu - 2023-10-12

    how about change BOL and EOL to only match real line start and end like MatchFlags:

    std::regex_constants::match_flag_type MatchFlags(const Document *doc, Sci::Position startPos, Sci::Position endPos) noexcept {
        std::regex_constants::match_flag_type flagsMatch = std::regex_constants::match_default;
        if (!doc->IsLineStartPosition(startPos))
            flagsMatch |= std::regex_constants::match_not_bol;
        if (!doc->IsLineEndPosition(endPos))
            flagsMatch |= std::regex_constants::match_not_eol;
        return flagsMatch;
    }
    
     
    • Zufu Liu

      Zufu Liu - 2023-11-24

      Patch to sync match_not_bol and match_not_eol, this fixed original problem except finding \> or \>$ at line end (will needs another patch similar to RESearch-EOW-atLineEnd-1013.diff).

      reverse search test cases for std::regex are omitted due to current MatchOnLines() treats empty match as failure.

       
      • Neil Hodgson

        Neil Hodgson - 2023-11-28

        The code for line end and line start seem intertwined so should be in a combined patch to better understand the scope.

         
        • Zufu Liu

          Zufu Liu - 2023-11-29

          Just following after switch in RESearch::Execute():

          if (ep == NOTFOUND) {
              /* similar to EOL, match EOW at line end */
              if (endp == lineEndPos && *ap == EOW) {
                  if ((ap[1] == END || ((ap[1] == EOL && ap[2] == END))) && iswordc(ci.CharAt(lp - 1))) {
                      lp = endp;
                      ep = lp;
                  } else {
                      return 0;
                  }
              } else {
                  return 0;
              }
          }
          

          so it's independent of other changes except test cases.

           
        • Zufu Liu

          Zufu Liu - 2023-11-30

          The combined patch:

           
          • Neil Hodgson

            Neil Hodgson - 2023-12-01

            I think there is a duplicated test for findingEOW on lines 600-603.

             
            • Zufu Liu

              Zufu Liu - 2023-12-01

              Removed the duplicated test.

               
  • Neil Hodgson

    Neil Hodgson - 2023-12-02

    Committed as [60a4cc] with extra tests for \>$.

     

    Related

    Commit: [60a4cc]

  • Neil Hodgson

    Neil Hodgson - 2023-12-02
    • status: open-accepted --> open-fixed
     
  • Neil Hodgson

    Neil Hodgson - 2023-12-25
    • status: open-fixed --> closed-fixed
     

Log in to post a comment.