bugGNU Octave - Bugs: bug #49451, matching beginning/end of a word...

 
 

bug #49451: matching beginning/end of a word does not work

Submitter:  Francesco Potortì <pot>
Submitted:  Thu 27 Oct 2016 01:43:01 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 04 Nov 2016 03:19:54 PM UTC, comment #3: 

I fixed regexp and regexprep to rewrite the pattern for double quoted strings (which was already being done for single quoted strings) before processing by PCRE.  This was done on the stable branch here (http://hg.savannah.gnu.org/hgweb/octave/rev/e6fac42c8d56).

There is still a small quirk in that the replacement string, when using double quoted strings, does not accept the matlab syntax for octal or hex.  Octave does accept ordinary syntax for octal and hex, and someone transporting code from Matlab to Octave would be using single quotes anyways where this already works.  In other words, I'm not too bothered by it, but if someone wants to change that as well they can.

Rik <rik5>
Group administrator
Thu 27 Oct 2016 04:12:25 PM UTC, comment #2: 

As a temporary workaround, use single quotes to avoid interpretation of escape sequences by the Octave interpreter.  This also tends to make the patterns more readable which is why I always do this.


regexprep("_   aaaa   _", '\<\w{4}', "b")
ans = _   b   _


The issue is likely to be that double quoted strings are being run through do_string_escapes twice.  First, by the Octave interpreter and then a second time by regexprep.

Rik <rik5>
Group administrator
Thu 27 Oct 2016 04:09:54 PM UTC, comment #1: 

Yeah, this is confusing to me as well. I remembered that I had submitted a fix for this a while ago.

Turns out that single-quoted strings and double-quoted strings are processed differently in Octave regexp functions. There are certain escape sequences that are handled only when the pattern string is a single-quoted string.

These include

  • \b means a literal backspace character (while it means word boundary in PCRE syntax)
  • \< \> mean word boundaries
  • \oNNN means a specific octal character


So as a workaround, if you use single quotes, then the angle-bracket word boundary anchors will work. Or use the PCRE word boundary anchor which is just \b .

Anyone else care to comment on the reason for relegating these certain escapes to single quoted strings only?

Mike Miller <mtmiller>
Group Member
Thu 27 Oct 2016 01:43:01 AM UTC, original submission:  

The following two expressions should give the same result, but they don't: the second is incorrect


> regexprep("_   aaaa   _", "\\w{4}", "b")
ans = _   b   _

> regexprep("_   aaaa   _", "\\<\\w{4}", "b")
ans = _   aaaa   _


Francesco Potortì <pot>

 

(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 rik5 (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by pot (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 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-11-04 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
        Release4.0.3 dev
    2016-10-27 rik5 StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code