
lovinPerl
New User
Jan 18, 2013, 6:02 PM
Post #1 of 5
(2720 views)
|
A simple regex question
|
Can't Post
|
|
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)
|