in reply to
Re: Re: Regex help
in thread Regex help
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:
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.