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: Regular Expressions:
Regex Help

 



zkr2133
New User

Sep 18, 2012, 5:14 PM

Post #1 of 7 (8074 views)
Regex Help Can't Post

Hey Guys

I am trying to find a way to match a phone number such as
7195551212, but I can use only "one" 5 in the search. I know how to do it with three 5's for the match expression but using just one 5 seems to be an impossible task.

My attempt so far:

Code
#!/usr/bin/perl 

@_ = (<>);

foreach $matchvar (@_){

if ($matchvar =~ /555/){
print "$matchvar\n";
}

}


Any help is appreciated.


(This post was edited by zkr2133 on Sep 18, 2012, 6:02 PM)


BillKSmith
Veteran

Sep 18, 2012, 6:30 PM

Post #2 of 7 (8051 views)
Re: [zkr2133] Regex Help [In reply to] Can't Post

This sounds like a homework assignment. No real application would care what syntax you use.

I suggest that you read the section QUANTIFIERS in perldoc perlreref.
Good Luck,
Bill


zkr2133
New User

Sep 18, 2012, 6:33 PM

Post #3 of 7 (8050 views)
Re: [BillKSmith] Regex Help [In reply to] Can't Post

Thanks will do


Laurent_R
Veteran / Moderator

Sep 18, 2012, 6:47 PM

Post #4 of 7 (8045 views)
Re: [zkr2133] Regex Help [In reply to] Can't Post

Yes, it does sound like a homework.

Still, I don't see why I should not try to help you if you do the rest of your homework.

Look at the regex quantifiers.

You can code something like this:


Code
do_something () if  ($foo =~ /d+5{3}\d*/)

;

This will detect 3 successive 5 anywhere provided previous values are also numbers (if there are 4 fives at the beginning, this will also match). Up to you to change it to be more precise in your regular expression depending on your exact needs.


zkr2133
New User

Sep 18, 2012, 6:49 PM

Post #5 of 7 (8044 views)
Re: [Laurent_R] Regex Help [In reply to] Can't Post

Thanks a lot I just figured it out before you posted your comment.. haha,
nonetheless it looks similar to yours, thanks for taking the time to answer my question.

This is what i figured out!

Code
 =~ /\d*5{3}/g



(This post was edited by zkr2133 on Sep 18, 2012, 6:51 PM)


zkr2133
New User

Sep 18, 2012, 6:54 PM

Post #6 of 7 (8040 views)
Re: [Laurent_R] Regex Help [In reply to] Can't Post

I have one question. What should I do if I want to find out numbers that specifically do not end in the range of 1-4? Is there anything specific that I should read?

Thanks


Laurent_R
Veteran / Moderator

Sep 18, 2012, 7:46 PM

Post #7 of 7 (8031 views)
Re: [zkr2133] Regex Help [In reply to] Can't Post

I was just saying that it would also match three fives in a row of more than three fives. In some cases, that is what you want; in others, you want exactly trhee. Depending on the case, your regex can match exactly what you want, but this isn't something I can decide for you. I chose one option, perhaps the other one is better, I do not know. But both can be done easily.

 
 


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

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