bugGNU Octave - Bugs: bug #59992, regexp: behaviour of \> (end of...

 
 

bug #59992: regexp: behaviour of \> (end of a word) inconsistent with MATLAB

Submitter:  Sébastien Villemot <svillemot>
Submitted:  Tue 02 Feb 2021 03:57:58 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 15 Feb 2021 06:02:32 AM UTC, comment #6: 

Thanks for the patch Rik.  Late, but confirmed that all examples I tried are fine with Matlab R2020b 👍

Kai Torben Ohlhus <siko1056>
Group Member
Fri 12 Feb 2021 06:16:20 PM UTC, comment #5: 

It's been 10 days.  I'm going to assume this patch is good.  Marking as fixed and closing report.

Rik <rik5>
Group administrator
Tue 02 Feb 2021 10:54:19 PM UTC, comment #4: 

I checked in a change on the development branch that fixes this (http://hg.savannah.gnu.org/hgweb/octave/rev/8f0d0d4690c0).  I had to change the pattern that '\>' is transformed into before passing to the PCRE library as outlined in comment #2.  I also had to add in the end-of-line character '$' since apparently this is what Matlab does.

Marking as Ready for Test.

Rik <rik5>
Group administrator
Tue 02 Feb 2021 09:02:17 PM UTC, comment #3: 

Matlab r2020b:

>> [b, e] = regexp ('foo!+bar', '\w+\>')
b =
     1     6
e =
     3     8
>> [b, e] = regexp ('foo?!+bar', 'foo?!\>')
b =
     []
e =
     []
>>


Philip Nienhuis <philipnienhuis>
Group Member
Tue 02 Feb 2021 07:27:01 PM UTC, comment #2: 

Confirmed.

Octave uses the PCRE library (Perl Compatible Regular Expressions) for regular expressions.  There is no end of word assertion in PCRE, so Octave substitutes '\b' which detects a Word Boundary.  A Word Boundary, however, is defined as word character followed by a non-word character.  Since '!' is not a word character the test fails between '!' and '+'.

It may be that Matlab is using a positive lookahead assertion for a non-word character such as '(?:\W)'.  That seems to work for this example.


Rik <rik5>
Group administrator
Tue 02 Feb 2021 06:33:43 PM UTC, comment #1: 

It looks like Octave is doing what Emacs does:


‘\>’
     matches the empty string, but only at the end of a word.
     ‘\>’ matches at the end of the buffer only if the contents
     end with a word-constituent character.

‘\w’
     matches any word-constituent character.
     The syntax table determines which characters these are.


While Matlab says


expr\>
      Matches:  The end of a word.
      Example:  '\w*e\>' matches any words ending with e.


The set of word-constituent characters in both Octave and Matlab appear to be the set [a-zA-Z_0-9], but I guess Matlab allows an arbitrary character to be considered as the final character in the word?  Can it be more than one?  For example, what do the following expressions do?


[b, e] = regexp ('foo!+bar', '\w+\>')
[b, e] = regexp ('foo?!+bar', 'foo?!\>')


Is there an easy way to get PCRE to work differently here and allow any character(s) to be treated specially as the end of a word?

John W. Eaton <jwe>
Group administrator
Tue 02 Feb 2021 03:57:58 PM UTC, original submission:  

Under Octave:

octave:1> regexp('foo!+bar', 'foo!\>')
ans = [](1x0)


While under MATLAB:

>> regexp('foo!+bar', 'foo!\>')

ans =

     1


Sébastien Villemot <svillemot>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by siko1056 (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by svillemot (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-02-12 rik5 StatusReady For Test Fixed
        Open/ClosedOpen Closed
    2021-02-02 rik5 StatusConfirmed Ready For Test
        Release6.1.0 dev
    2021-02-02 rik5 StatusNone Confirmed
        Operating SystemGNU/Linux Any

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code