Alessandro,


Alessandro Lenzen wrote:
>--------------------------------
>#!/usr/bin/perl -w
>use strict;
>
>my($string) = "This is a test!";
>my($ergebnis);
>
>$string =~ m/(\s\w+\s)/g;
>$ergebnis = $1;
>print("$ergebnis\n");
>--------------------------------
>
>Shouldn't a be printed?

I'm assuming, first of all, that you expect an ' a ' character to be 
printed. (It's not totally clear from your posting that this is what
you meant, but it's the most reasonable translation I can make. :) )

Before any analysis, I'd like to point out an important rule
of regular expressions (taken from _Mastering Regular Expressions_
by Jeffrey E. F. Friedl): 

         The earliest match wins.
         ------------------------

So now let's look at the regular expression:

\s   indicates a single whitespace character
\w+  indicates one or more word characters
\s   indicates a single whitespace character

And to recap, the test string is 'This is a test!'.

So what we're attempting to match is a sequence of one or more word
characters bounded on both sides by a single whitespace character.
Both ' is ' and ' a ' fit this description.  Because _the earliest
match wins_, ' is ' is matched rather than ' a '. (Whitespace is
included in the $1 due to your placement of parentheses.)

If you wanted to get ' a ' instead, you'd have to change '\w+' to
'\w'.  And if you wanted 'a' rather than ' a ', you'd use the
expression '\s(\w)\s' (excluding single-quotes, of course) to
grab *only* the word character.

Does that help?

John
-- 
               John Fox <[EMAIL PROTECTED]>
    System Administrator, InfoStructure, Ashland OR USA
  ---------------------------------------------------------
"What is loved endures.  And Babylon 5 ... Babylon 5 endures."
              --Delenn, "Rising Stars", Babylon 5

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to