in reply to Re-define Word Boundary?

What I'd like to do is redefine what "\b" is.

You can, by overloading regular expressions. The following is just a quick hack I threw together, but I think it catches most cases, including /[\b]/ and /\\b/.

#!/usr/bin/perl use strict; use warnings; no warnings qw /syntax/; BEGIN { my $B = '(?:(?<=[\w/])(?=[^\w/])|' . '(?<=[^\w/])(?=[\w/])|' . '^(?=[\w/])|(?<=[\w/])$)'; $^H |= 0x30000; $^H {qr} = sub { local $_ = $_ [1]; s/(\[\^?]?[^]]*]|[^\\]+|\\.)/$1 eq '\b' ? $B : $1/eg; $_; } } while (<DATA>) { chomp; print "$_: "; print $& if /\b\w+\b/; print "\n"; } __DATA__ foo &foo& &foo/bar& foo: foo &foo&: foo &foo/bar&:

Abigail

Replies are listed 'Best First'.
Re: Re: Re-define Word Boundary?
by ysth (Canon) on Nov 21, 2003 at 01:05 UTC
    Please don't do that, unless you are planning on sticking with the same (tested) perl version forever. perldoc perlvar says:
    $^H WARNING: This variable is strictly for internal use only. Its availability, behavior, and contents are subject to change without notice.
    and
    %^H WARNING: This variable is strictly for internal use only. Its availability, behavior, and contents are subject to change without notice.
    See perldoc perlre "Creating custom RE engines" for the documented way to overload regular expressions.