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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Regular expression...again

 



Rivotti
User

Apr 13, 2000, 5:02 PM

Post #1 of 6 (923 views)
Regular expression...again Can't Post

I'm tryng to check if a string matches one of the following:

10.10.10
10/10/10
10-10-10
10 10 10
10.10.1000
10/10/1000
10-10-1000
10 10 1000

If tryed this like:

$field=~ /^\d{2}\/|\.|\-| \d{2}\/|\.|\-| \d{2}|\d{4}$/

or

$field=~ /^\d{2}\(\/|\.|\-| )\d{2}\(\/|\.|\-| )\(\d{2}|\d{4})$/

or

$field=~ /^\d{2}\(/|.|-| )\d{2}\(/|.|-| )\(d{2}|d{4})$/

or

$field=~ /^\d{2}\[\/|\.|\-| ]\d{2}\[\/|\.|\-| ]\[\d{2}|\d{4}]$/

or

$field=~ /^\d{2}\[/|.|-| ]\d{2}\[/|.|-| ]\[d{2}|d{4}]$/

What I'm I doing wrong? How to do what I want? Can you explain me the concept of alternation in regular expression?

Thanks in Advance
Rivotti


Cure
User

Apr 13, 2000, 9:51 PM

Post #2 of 6 (924 views)
Re: Regular expression...again [In reply to] Can't Post

Hi
$filed_=~ m!\d{2}[- /.]\d{2}[- /.]\d{2,4}!

I changed the delimiter to a ! (m!regular expression!) because I already have 2 forward slashes in the regular expression.

so \d{2} means match 2 digits
[- /.] means match - or space or / or .
\d{2,4} means match 2 to 4 digits

Cure



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


japhy
Enthusiast

Apr 13, 2000, 10:40 PM

Post #3 of 6 (923 views)
Re: Regular expression...again [In reply to] Can't Post

You can't do [\d{2}\d{4}] to mean "2 digits or 4 digits". Character classes are "one of the following CHARACTERS". You need to use alternation (\d{2}|\d{4}) to get the desired effect.


Rivotti
User

Apr 14, 2000, 1:46 AM

Post #4 of 6 (923 views)
Re: Regular expression...again [In reply to] Can't Post

Thanks Cure: Smile

What "m!" does? Another thing: \d{2,4} means 2 to 4 numbers. What if the condition was 2 or 4 number?
If I use "101 10 0000", "10 101 0000" or "10 10 10000" it still validade. I whant the input to be exactly a mask like: XX("." or "/" or "-" or " ")("." or "/" or "-" or " ")XX or XXXX.

Rivotti

[This message has been edited by Rivotti (edited 04-14-2000).]


Cure
User

Apr 14, 2000, 9:40 AM

Post #5 of 6 (923 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



Rivotti
User

Apr 14, 2000, 2:54 PM

Post #6 of 6 (923 views)
Re: Regular expression...again [In reply to] Can't Post

I'm glad to have you helping me. Thanks for the lesson, Cure and Japhy, it was very usefull. Smile

Keep the good work and this excellent Forum
Rivotti

 
 


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

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