s/ and #

A

amyl

I came across a substitution that used the "#" character as a
replacement for the "/" character.

$nuri =~ s#^(https?:)/{0,2}#$1//#i;

In this case what exactly does the "#" do? Why use it opposed to "/"?
I guess the only thing I can think of is that it makes the regular
expression easier to read since its working with forward slashes.

Thanks
Amy.
 
P

Paul Lalli

I came across a substitution that used the "#" character as a
replacement for the "/" character.

$nuri =~ s#^(https?:)/{0,2}#$1//#i;

In this case what exactly does the "#" do? Why use it opposed to "/"?
I guess the only thing I can think of is that it makes the regular
expression easier to read since its working with forward slashes.

That's it exactly. All of the operators that are traditionally written
with slashes (s///, m//, tr///, q//, qq//, qw//, qx//, qr//) can
actually use any non-alphanumeric character as the delimiter. And
that's precisely the reason - if you need to use / as a character
within the operation, you'd have to backslash it if it's also the
delimiter. By choosing an alternate delimter, you don't have to
backslash it, and it's easier to read.

A few caveats:
* You always have to backslash the delimiter. So if your search
operation above was looking for an actual #, you'd have to backslash
it. In general, just don't choose a delimiter that the expression
actually uses.
* With the m// operator, ? is not an ordinary delimiter. It causes the
pattern match to be evaluated only until it becomes true (ie, if it's
in a loop block). After that, it's ignored. Unless you specifically
want this rather bizzare functionality, don't use ? as a delimiter
* Try to avoid any character as a delimiter which is also a regexp meta
character that you'd need to use in your expression. For example, if
your expression uses alternation, don't use the | as a delimiter
* If you use any of (, {, [, or < as the opening delimiter, you must
use the corresponding ), }, ], or > as the closing delimiter.
* A corrolary to that - in the s/// operation, you can use different
bracket-like pairs for the two parts of the expression (the search and
the replace). This means that you will actually use a total of four
characters for the delimiters, rather than three. For example:
$string = s{foo}[bar]g; # replace all 'foo' with 'bar'
* In the m// operator, the 'm' is optional *only* if you use / as the
delimiter. If you choose any other delimiter, the 'm' is required.
That is:
$foo =~ m/bar/;
can be written:
$foo =~ /bar/;
but
$foo =~ m!bar!;
CANNOT be written
$foo =~ !bar!;

Hope this helps,
Paul Lalli
 
A

Ala Qumsieh

Paul said:
That's it exactly. All of the operators that are traditionally written
with slashes (s///, m//, tr///, q//, qq//, qw//, qx//, qr//) can
actually use any non-alphanumeric character as the delimiter.

Wrong. You can use *any* character:

% perl -wle '$_ = "ala"; s tatxtg; print'
xlx

note that I don't recommend anybody doing this.

--Ala
 
P

Paul Lalli

Ala said:
Wrong. You can use *any* character:

% perl -wle '$_ = "ala"; s tatxtg; print'
xlx

Well. That's just disturbing. Thanks for the corrected information,
Ala.

Paul Lalli
 
J

John Bokma

Paul Lalli said:
* With the m// operator, ? is not an ordinary delimiter. It causes the
pattern match to be evaluated only until it becomes true (ie, if it's
in a loop block). After that, it's ignored. Unless you specifically
want this rather bizzare functionality, don't use ? as a delimiter

Additionally (perldoc perlop):

" If "'" is the delimiter, no interpolation is performed on the PATTERN. "
* A corrolary to that - in the s/// operation, you can use different
bracket-like pairs for the two parts of the expression (the search and
the replace). This means that you will actually use a total of four
characters for the delimiters, rather than three. For example:
$string = s{foo}[bar]g; # replace all 'foo' with 'bar'

Additionally, this also works for m, e.g. m{foo}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top