SPUG: \b \w \s etc. Re: regular expressions

Fred Morris m3047 at inwa.net
Tue Jan 17 14:29:40 PST 2006

\b is a zero-width assertion, meaning that it doesn't consume anything. It
matches between what \W\w or \w\W would match.

\w ("word") is not quite the opposite of \S ("not space"), due to
punctuation, etc. Contrast these:

echo "testing's tricky" | perl -pe 's/((?:^|\W)\w)/\U$1/g'

echo "testing's tricky" | perl -pe 's/((?:^|\s)\S)/\U$1/g'


Fred Morris

On Tue, 17 Jan 2006, Randy Rue wrote:

> What does the (\w) in the pattern do to the \b word separator? I thought
> it might give whitespace as the word delimiter (and so keep words like
> "that's" as a single word) but the output still comes out as "That'S."
> How else could I treat contractions?
> Randy Rue
> Jeremy Mates wrote:
> > * luis medrano <lmzaldivar at gmail.com>
> >
> >>perl is number one
> >>to
> >>Perl Is Number One
> >
> >
> > Quick, dirty, and possibly missing all sorts of edge cases:
> >
> > echo test ing | perl -pe 's/\b(\w)/\U$1/g'
> >

More information about the spug-list mailing list