Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using REGEX - question.

Using REGEX - question.

Using REGEX - question.

I have the following that I am using in order to find as many matches as possible and then print out the number of matches found, and which letters were found in the match.

Here is what I have:

 $letters5 = "AARON FOOBARR SMITTH";
 if( $letters5 =~ m/\w*(.)\1\w*/g ){
 print "\n";
 print "matched $&\n";
 print "1 = $1\n";
 print "\n\n";

But, I get the same result with or without the  /g  at the end.  I am trying to figure out how to have the match (maybe re-written) so it will not only match the first set of double letters - but as many as it can find. Then, print out for me the total number of matches found, and which words contained the matches ... and which letters were matched.

Help ... please?


PS:  What am I doing wrong here?

RE: Using REGEX - question.

Just use 'while' rather than 'if'.

while ($letters5 =~ /\w*(.)\1\w*/g)
# print stuff

 keep the rudder amid ship and beware the odd typo

RE: Using REGEX - question.

if you just want the number times it matches, evaluate the regular expression with 'g' on, assign that to an array, then evaluate that array in scalar context:[color]
@matches = m/PATTERN/g;
$count = scalar(@matches);

however, this has only two situations that it will work.  one is if there are no parantheses enclosed substrings in the regex, in which case the elements of the array will be the $& values of each match.  the other is if there is only one set of parentheses, in which case the array will be the values of $1 for each match, and $& will be unavailable.  if there is more than one set of parentheses, the array will be ($1, $2, $3, $1, $2, $3, ...), with one entry for each parens in every match.  you could then break this apart, so you could effectively have your $& by:

in which case $1 would be the equivelent of $&, and $2 would be the little snippit, but this would make the array it returns less ledgible and harder to deal with.  if you need access to $& and $1, your loop works well.  you can set an iterator ($i++;) in the loop to count the number of matches.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Using REGEX - question.


Thanks.  Your answer is a little over 'my' head at this point, but I will look it over and try to understand it.  Thanks for the additional feedback!!  )  Always appreciated.


Gary M. Gordon, LLC
Certified Web Developer ::
Application Programmer

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close