in reply to quite weird regexp

You can use any non-alphanumeric character as the separator in a s/// or m// operator. This allows you to choose a separator which doesn't clash with the data in the string.

In this case, if you used the 'traditional' slash as the delimiter then you'd need to escape the slash that appears in the regex. By changing the separator, you don't need to do that.

You can also use 'bracketing' characters, like (, [, < or {. In that case you need to use the opposite character at the other end, like this:

s[something][else];

This is all explained in perldoc perlop.

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: quite weird regexp
by chipmunk (Parson) on Nov 30, 2000 at 19:33 UTC
    What perlop doesn't tell you is that, if you put a space after the operator name, you can use alphanumeric characters as delimiters. To use the current example: ($Z = $0) =~ s z.*/zz; I also made use of this in homer.pl, with m mmm, Donuts. But please don't do this in production code! :)
Re: Re: quite weird regexp
by clemburg (Curate) on Nov 30, 2000 at 19:45 UTC

    To be precise (citing from perlop manpage):

    • Whitespace is not a valid delimiter, and there are additional semantics associated with single quotes:
      s/PATTERN/REPLACEMENT/egimosx [ ... snip ...] Any non-alphanumeric, non-whitespace delimiter may replace the slashes. If single quotes are used, no interpretation is done on the replacement string (the /e modifier overrides this, however). Unlike Perl 4, Perl 5 treats backticks as normal delimiters; the replacement text is not evaluated as a command. If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own pair of quotes, which may or may not be bracketing quotes, e.g., s(foo)(bar) or s<foo>/bar/. A /e will cause the replacement portion to be interpreted as a full-fledged Perl expression and eval()ed right then and there. It is, however, syntax checked at compile-time.
    • with m//, you also want to watch out not to use '?' as the delimiter, as this has a different meaning:
      ?PATTERN? This is just like the /pattern/ search, except that it matches only once between calls to the reset() operator. This is a useful optimization when you want to see only the first occurrence of something in each file of a set of files, for instance. Only ?? patterns local to the current package are reset. [ ... snip ...] m/PATTERN/cgimosx /PATTERN/cgimosx [ ... snip ...] If "/" is the delimiter then the initial m is optional. With the m you can use any pair of non- alphanumeric, non-whitespace characters as delimiters. This is particularly useful for matching Unix path names that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is the delimiter, then the match-only-once rule of ?PATTERN? applies. If "'" is the delimiter, no variable interpolation is performed on the PATTERN.

      This is all explained in perldoc perlop (there is no "perlops" manpage).

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

RTF(antastic)M (Re: Re: quite weird regexp)
by larsen (Parson) on Nov 30, 2000 at 15:14 UTC
    Thank you very much.
    (Alas, perhaps I count from 1 to 10 too quickly :) )