Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Regex help

by fglock (Vicar)
on Oct 01, 2002 at 20:34 UTC ( #202098=note: print w/ replies, xml ) Need Help??


in reply to Re: Regex help
in thread Regex help

You mean  (.*?)

Actually you don't need parenthesis:

s/<script>.*?<\/script>//sg;


Comment on Re: Re: Regex help
Select or Download Code
Re: Regex help
by Anonymous Monk on Oct 01, 2002 at 23:40 UTC
    Nope. The parenthesis are optional, but can be VERY useful. For example, say you want to remove the <script> and </script>, but be able to give some sort of warning about the script tags. For example, you may filter out:
    <script> malicious_code_to_do_something_nasty </script>
    If you use your regex as <script>(.*?)</script>, it saves the smallest amount (the ?) of anything (the .*) into a variable. That variable name depends on how many sets of parenthesis you've used. If it's the first (and only) time you use them, it gets saved into $1. If the second time, $2, and so forth. You can use it for something like this:
    $text = "my name is john q user\n"; $text =~ s/^my name is (.*?) .*$/$1/; # removes "my name is ", saves the next word, essentially, into $1, re +moves the rest print "hello, $text!\n"; # prints "hello, john!\n"
    This is VERY useful in extracting information from strings.


    -dingoStick.com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://202098]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2015-07-06 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls