[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] REgular Expression for IP address



On Sun, Feb 13, 2000 at 12:49:13AM +0100, Detlef Lindenthal wrote:
> =Ronald wrote:
> > On Sat, Feb 12, 2000 at 03:33:54PM +0100, Detlef Lindenthal wrote:
> > > $IPN =~ s/\./x/g;
> > > @IPN = split "x", $IPN;
> > >     ## Sorry for these two lines -- but how could I have split at "."??
> > >     ## This does not work:  @IPN = split ".", $1;
> > >     ##      nor does this:  @IPN = split "\.", $1;
> 
> > The same way you change the periods to Xs.  The first argument to split is
> > a _regular expression_.
> 
> > @IPN = split "\\.", $1;   # workable
> > @IPN = split "\.", $1;    # also workable
> > @IPN = split /\./, $1;    # PREFERRED
> > @IPN = split "\.", $1;
> > Ronald
> 
> 
> Hi Ronald,
> 
> thank you for repairing. But I do not agree completely:

It's a bit tricky.  I will try to explain further.


> =cut
> 
> $_ = "asdf.asdf.asdf.asdf";
> s;a;aa;g;   ## here even   ;   is allowed.
> print "\n(1.) ", join " / ", split "\." , $_; ## not workable on my machine!!

That depends what you mean by "workable".  This is equivalent to:

split /./, $_

which splits on any character.  It works, it's just not what was intended.


> print "\n(2.) ", join " / ", split '\.', $_;  ## works fine
> print "\n(3.) ", join " / ", split "\\.", $_; ## works fine -- why??

Both of those are equivalent to:

split /\./, $_

which splits on literal periods


> print "\n(4.) ", join ' / ', split /\./, $_;  ## works fine


> print "\n(5.) ", join " / ", split /\\./, $_; ## does not split

This is:

split /\\./, $_

which splits on a literal backslash followed by any character.


> #print"\n(6.) ", join " / ", split |\.|, $_;  ## would yield a syntax error

It's a syntax error because |\.| is not a syntactically valid expression.
split m|\.|, $_
would be valid, however.


> =You  write:
> 
> > The first argument to split is a _regular expression_.
> 
> If so -- why can RE in this case be delimited by " , whereas in other
> cases " is not allowed? Are there different classes of RE or classes of
> use of RE? -- If this topic is tooo special, then forget my question. I
> am glad that my script works :-)

Quotes always delimit strings.  The significance here is that when Perl is
expecting a search pattern and gets an expression instead, the expression
is evaluated, and treated as a search pattern.

Note the following, from the documentation for =~ in perlop:

       (If the right argument is an expression rather than a search
       pattern, substitution, or transliteration, it is interpreted as a
       search pattern at run time.  This can be is less efficient than an
       explicit search, because the pattern must be compiled every time the
       expression is evaluated.

Since the first argument in split "\.", $_ is not a search pattern, it is
evaluated as an expression, which results in the string '.'.  This string
is then compiled into a search pattern, which matches any character.

Similarly, you could write $_ =~ "\." to match the first character in $_
(other than a newline).

To avoid confusion, you should always use // or m// with the first argument
to split.  (Except for " ", which is a special case.)


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org