Shorthands
In Perl, \b = word boundary, but withing a character class, \b = backspace.
#!/bin/perl -l
=begin
\b normaly matches a word boundary
but, withing a character class, \b matches a backspace
\s is a shorthand for a whole character class, any whitespace character
This includes space, tab, newline, and carriage return
Among the more useful shorthands that Perl provide are:
\t tab character
\n newline character
\r carriage-return character
\b backspace
\s any whitespace character (space, tab, newline)
\S anything not s
\w [a-zA-Z0-9_]+ match a word with w+
\W [^a-zA-Z0-9_]+ anything not w
\d [0-9] a digit
\D [^0-9] anything not d
=cut
$s = '-98 F';
$p = '^([-+]?[0-9]+)\s*[CF]$'; # \s*
if( $s =~ m/$p/ ) {
print $1; # '-98'
}
Questions and answers
Find any whitespace character
- a) \s
- b) \w
Which one matches in Perl a word boundary?
- a) \b
- b) [\b]
Find any non whitespace character
- a) \s
- b) \S
Find any non word
- a) [a-zA-Z0-9_]+
- b) \W