Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular Expression Newbie Question

Reply
Thread Tools

Regular Expression Newbie Question

 
 
ahjiang@gmail.com
Guest
Posts: n/a
 
      04-12-2006
Hi all,

I have a few simple question here

$program = "/*Hello world*/ How are you";

$program =~ s {
/\* # Match the opening delimiter.
.*? # Match a minimal number of characters.
\*/ # Match the closing delimiter.
} []gsx;


Output: How are you

I understand im using the s/PATTERN/REPLACEMENT/egimosx this.

1) Why does the above syntax still works. It doesnt follow s///.
2) \* Match the opening delimiter. The opening delimiter in $program is
/* how come \* still matches it?

Appreciate any help.

 
Reply With Quote
 
 
 
 
niall.macpherson@ntlworld.com
Guest
Posts: n/a
 
      04-12-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>
> 1) Why does the above syntax still works. It doesnt follow s///.
> 2) \* Match the opening delimiter. The opening delimiter in $program is
> /* how come \* still matches it?
>
> Appreciate any help.



1) You can use almost any char as a delimiter in a substitution (or a
match for that matter) - in most cases you will see s/// but you will
sometimes see different ones being used

>From perldoc perlrequick


----------------------------------------------------------------------------------------------------------------------
# convert percentage to decimal
$x = "A 39% hit rate";
$x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"

The last example shows that "s///" can use other delimiters, such
as
"s!!!" and "s{}{}", and even "s{}//". If single quotes are used
"s'''",
then the regex and replacement are treated as single quoted
strings.
----------------------------------------------------------------------------------------------------------

2) The '\' is escaping the first '*' since '*' is a special character
in a regex. Therefore it is just looking for a match on '*' not '\*' .
Similarly for the second '*'.

Hope this helps

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments