Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slashes In Regular Expressions

Status
Not open for further replies.

AMiSM

Technical User
Joined
Jan 26, 2006
Messages
128
Location
US
I just started in Perl, and the whole concept of regular expressions is very cool! I like the efficiency!
I came across a problem, and although I found a different way to do what I was doing, I'd like to get this knocked out.
Can sombody please shed some light on ecaping charactors in regular expressions? Do you have to escape the final slash sometimes? This made me wonder:



What's wrong here?:

for (@lines) {
if ( m//<A HREF=/"//news/// ) {
print $_;
}
}
 
Escape character is used to suppress special meaning of other special characters.

in the line
Code:
   if ( m//<A HREF=/"//news/// ) {
/ and " are special characters and need to be escaped with \ to retain their meanings.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
For a start, you're using the wrong 'slash' as your escape character. The backslash (\) is used to escape.

Then, '<' doesn't need escaping...

m/<A HREF=\"\/news\//

.. seems to be what you are after; a match on: <A HREF="\news\

 
If slashes are bothering you, try running your expressions with a different delimiter
Code:
  if ( m#<A HREF=\"news# ) {
            print $_;
      }

when using alternate delimiters m# # or s# # #, the m or s is required. When using the default / / or / / / the m or s is optional.

Jeb
\0
 

Yeah, the wrong slash. (duh!)

Thanks
 
I'd also like to point out that in addition to using the pound sign (#) or forward slash as delimiters for regex matches, e.g.

m# ... regex ... #

You can use just about any punctuation symbol instead of the pound or slash. For example,

m% ... regex ... %

should also work. This flexibility helps readability depending on what regex you're trying to match. I'm relying on memory here, but I think you can also use brackets, parens, and angle-brackets provided you use the closing one at the end, e.g.

m< ..regex .. >

(Too lazy to test at the moment, let me know if memory misleads me).
 

Yeah, I think I remember reading that somewhere. Time to try it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top