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: A simple regex question: Edit Log



lovinPerl
New User

Jan 18, 2013, 6:02 PM


Views: 906
A simple regex question

This question is going to seem super basic (because it is)

pretty much, I have these codes, and they can be of the following two forms, (where a represents any letter):

aa* or a*a

So now, I know how to do pattern matching and I could do something like (/\w{2}\*/) - this would match the first type. However, I'm confused how to get the second type. I can't do (/\w\*\w/) because this would match a*b, or b*c, or g*l, or anything like that. The issue is the first and second letter must be the same. So, b*b is all right, z*z is all right, etc.


Edit: Think I figured it out! There is something in perl called pattern memory. I have done this:

if(m/(\w)\1\*/){
print "matched case one with $1";
}
if(m/(\w)\*\1/){
print "matched case two with $1";
}

this works! Apparently putting something in parens stores it as a variable, first $1, then $2, etc.

for some reason when I try to combine the two if statements by using brackets to give the OR condition,

if(m/[(\w)\1*/|(\w)\*\1]/){
print "print $1";
}

this doesn't seem to work


(This post was edited by lovinPerl on Jan 18, 2013, 7:14 PM)


Edit Log:
Post edited by lovinPerl (New User) on Jan 18, 2013, 6:02 PM
Post edited by lovinPerl (New User) on Jan 18, 2013, 7:14 PM


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

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