CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Regular Expression



Apr 20, 2000, 2:48 AM

Post #1 of 3 (1013 views)
Regular Expression Can't Post


What is the regular expression to force a string to match this:




Apr 20, 2000, 5:40 AM

Post #2 of 3 (1013 views)
Re: Regular Expression [In reply to] Can't Post

 This is to find out if a string only has alphabetical characters.

if($string =~ /[A-Za-z]/)
print "This string only has letters between a and z of any case";
print "this string has characters other than alphabetical ones.";

you could add more to find out if the string has other characters, like numbers.

if(($string =~ /[A-Za-z][0-9]/) && ($string =~ /\\\\\\%\\/))
print "This string only has all kinds of characters, but only the ones that I will allow.";}
print "this string has characters other than ones that I allow.";

if this doesn't work you can also use the matching operator.

@filter = (
other character you don't want in $string,

foreach $key (@filter) {
if ($sting =~ m/$key/i)
print "there is a character you don't want in $string";

the i after m/$key/ means to ignore case.

Hope this helps you out,


[This message has been edited by perlkid (edited 04-20-2000).]


Apr 20, 2000, 7:25 AM

Post #3 of 3 (1013 views)
Re: Regular Expression [In reply to] Can't Post

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:

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:

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

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

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).

so the back reference variable would be $1,$2,$3,$4


[This message has been edited by Cure (edited 04-20-2000).]


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives