
Cure
User
Apr 14, 2000, 9:40 AM
Post #5 of 6
(340 views)
|
|
Re: Regular expression...again
[In reply to]
|
Can't Post
|
|
Hi The pattern-match function is used with regular expressions to find patterns in a variable or string. The syntx of the patten-match function is: m/pattern/ The two forward slash chracters delimit the pattern to be matched. The m is not required; you will frequently see the pattern-match function like this: /pattern/ The m has one significant use, however. Perl 5 uses the first character following the m as the delimiter for the pattern. When you want to match strings that contain foward slashes, such as s directory seperatorm you can the defualt delimiter(/) to another value, like this m!pattern! The m//, s///, and split functions operate on the default input special variable($_), which is the default storage variable on the last operation. If you don't want to match against the default input special variable, you need to use the binding operator(=~) with the variable you wish the regualr expression to match against. The binding operator tells the function to match agianst the vairable on the left side of the expression instead of the defualt input special variable. The following standard quantifiers are recognized. ^ match the beginning of new line. $ match the end of the line. . match any character (except newline). * match 0 or more times. + match 1 or more times. ? match 1 or 0 times. {n} match exaclty n times. {n,} match at least n times. {n,m} match at least n times but nore more than m times. () Grouping [] Character Class So to ask your querstion about \d{2,4} means 2 to 4 numbers. What if the condition was 2 or 4 number? you could do it by grouping (\d{2}|\d{4}) or by Character Class [\d{2}\d{4}] The parenthesis around a regular expression also has another effect. The string matched by a regular expression in parenthesis is saved in a back referenece varaible. A back refernece variable is a variable created during regular expression pattern match. The back reference variable contains the string match by the pattern enclosed by the parentheses. The back reference variable is always a variable of the name $1 to $n. (n is always equal to the number of the parentheses pairs used in the regular expression). example. /(P1)(P2)(P3)BETA(P4)/ so the back reference variable would be $1,$2,$3,$4 $1=P1 $2=P2 $3=P3 $4=P4 Cure
|