On Thu, Jul 22, 2010 at 12:57 AM, Chandan Kumar <chandan_28...@yahoo.com> wrote:
> ex: $_="#!chk/usr/bin/perl";
>
> 1)The output for ---- (\b\W\b) is \

There is no \ (back-slash) character in your string. :\ I assume then
that you meant '/' (forward-slash)?

> Iam looking for some character which is between a word character and non-word 
> character,
> To my understanding I'm expecting  ! as my asnwer. because starting of the 
> string there is # (non-word character) next !  (non word character) next word 
> character " c ".
> Then answer should be ! ,Right?  can any one explain.
>
> Why does it showing result as \ which is between 2 word characters .

As Uri explained, a word boundary is the point between a word
character and a non-word character. # and ! are both non-word
characters so there is no word boundary between them. The first word
boundary is between ! (non-word character) and c (word character). The
first matching pattern is the forward slash in "k/u" because there is
a word boundary between k (word character) and / (non-word character)
and also between / (non-word character) and u (word character) and /
is between them and also a non-word character.

> 2) For the same example can any one explain (\b\W) and (\W\b)? Is these are 
> valid?

(\b\W) would match a word boundary followed by a non-word character.
An example pattern that we would expect to match is the "!" in "foo!".

(\W\b) is just the opposite, first matching the non-word character and
then matching the word boundary. An example pattern that we would
expect to match is the "(" in "(foo".

I appear to be correct, according to this rudimentary test (note that
I am still new to writing Perl so hopefully I'm not misinterpreting
the results):

  use strict;
  use warnings;

  use Data::Dumper;

  my @data = ('foo!', '(foo');
  my @regexes = ('(\b\W)', '(\W\b)');

  for my $r (@regexes)
  {
      for my $d (@data)
      {
          my ($match) = $d =~ m/$r/;

          print "$d =~ /$r/ matches $match\n" if defined $match;
      }
  }

Output:

foo! =~ /(\b\W)/ matches !
(foo =~ /(\W\b)/ matches (


-- 
Brandon McCaig <bamcc...@gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to