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:
Matching 2 same characters

 



paragkalra
Novice


Mar 19, 2009, 10:57 AM

Post #1 of 7 (3381 views)
Matching 2 same characters Can't Post

I have array containing many words. I just want those words out of that array which contain same characters side by side e.g 'aa', 'bb', '11', '!!', 'zz' etc

So I want the regular expression that should be able to match only words of following type:

Google, Bee, Dell, Linux99, s@@
Parag.A.Kalra, The Linux Man
http://www.paragkalra.com/
In this world without WALLS and GATES who need WINDOWS! USE LINUX! STOP PIRACY! GO OPEN SOURCE!


KevinR
Veteran


Mar 19, 2009, 11:45 AM

Post #2 of 7 (3379 views)
Re: [paragkalra] Matching 2 same characters [In reply to] Can't Post

Please post the code you have written to try and solve your requirements, even if it does not work.
-------------------------------------------------


paragkalra
Novice


Mar 19, 2009, 12:08 PM

Post #3 of 7 (3377 views)
Re: [KevinR] Matching 2 same characters [In reply to] Can't Post

foreach (@myarray) {
if ( $_ =~ /(.){1}/) {
print "$_ \n";
}
}
Parag.A.Kalra, The Linux Man
http://www.paragkalra.com/
In this world without WALLS and GATES who need WINDOWS! USE LINUX! STOP PIRACY! GO OPEN SOURCE!


KevinR
Veteran


Mar 19, 2009, 12:14 PM

Post #4 of 7 (3376 views)
Re: [paragkalra] Matching 2 same characters [In reply to] Can't Post

All that does is check if $_ has one of any character.


Code
foreach (@myarray) {  
if ( $_ =~ /(.)\1/) {
print "$_ \n";
}
}


I leave it up to research \1
-------------------------------------------------


paragkalra
Novice


Mar 19, 2009, 2:09 PM

Post #5 of 7 (3371 views)
Re: [KevinR] Matching 2 same characters [In reply to] Can't Post

Ok one more question. Suppose I want to match words containing exactly 5 characters. I am trying this:

$_ =~ /(.){5}/

However it is getting me even words those words having more than 5 characters.
Parag.A.Kalra, The Linux Man
http://www.paragkalra.com/
In this world without WALLS and GATES who need WINDOWS! USE LINUX! STOP PIRACY! GO OPEN SOURCE!


KevinR
Veteran


Mar 19, 2009, 2:15 PM

Post #6 of 7 (3370 views)
Re: [paragkalra] Matching 2 same characters [In reply to] Can't Post

thats because you are matching a substring which means it can match anywhere. You can look up string anchors and specifically the word boundary anchor \b to do what you want.
-------------------------------------------------


paragkalra
Novice


Mar 19, 2009, 2:21 PM

Post #7 of 7 (3369 views)
Re: [KevinR] Matching 2 same characters [In reply to] Can't Post

Thanks Kevin...I got it working as suggested by you:
$_ =~ /^(.){5}$/
Parag.A.Kalra, The Linux Man
http://www.paragkalra.com/
In this world without WALLS and GATES who need WINDOWS! USE LINUX! STOP PIRACY! GO OPEN SOURCE!

 
 


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

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