Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re-define Word Boundary?

by thelenm (Vicar)
on Nov 20, 2003 at 23:27 UTC ( #308753=note: print w/replies, xml ) Need Help??


in reply to Re-define Word Boundary?

\w matches alphanumerics and underscore. \b is effectively the same as using lookbehinds and lookaheads like this:

(?:(?<=\w)(?=\W|\z)|(?:(?<=\W)|(?<=\A))(?=\w)

Update: Hmm, or even nicer, as merlyn posted in Re: Why do zero width assertions care about lookahead/behind? (code examples also updated),

(?:(?<!\w)(?=\w)|(?<=\w)(?!\w))

So to make a specialized version of \b that views "-" and "/" as "word characters" (sort of), you might use something like this:

(?:(?<![\w/-])(?=[\w/-])|(?<=[\w/-])(?![\w/-]))

So maybe something like this will suit you?

my $w = '\w/-'; my $b = "(?:(?<![$w])(?=[$w])|(?<=[$w])(?![$w]))"; my @words = ($rec =~ /${b}[$w]+${b}/g);

I've tested this a little but not a lot, and it seems all right. You'll want to verify it yourself before you go using it for anything important :-)

-- Mike

--
XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
-- grantm, perldoc XML::Simpler

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://308753]
help
Chatterbox?
Discipulus Anonymous is fast and ironic
[Corion]: From time to time, you actually uncover things that always were wrong ;)
[Happy-the-monk]: Discipulus: please, what are the labels for CTRL+SHIFT+TAB on an Eatalian keyboards?
[Discipulus]: yes but ireally do not understand anonymous's reply about cpan r command: found no bug with that number and points to mjd.. doh?!

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-11-21 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (297 votes). Check out past polls.

    Notices?