How to match repeat characters in Perl?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BethL
    New Member
    • Nov 2007
    • 1

    How to match repeat characters in Perl?

    Hello,

    I'm new to Perl and I'm stepping through an online tutorial. I'm trying to count the lines in a file that have strings with double letters in them. This is my code -

    [CODE=perl]$file = 'c:\Perl\Beth\f 7.txt';
    open(INFO, $file);
    $i = 1;
    while ($line = <INFO>)
    {
    $_ = $line;
    if (/.+/)
    {
    print "00$i $line";
    $i++;
    }
    }
    close(INFO);[/CODE]
    "f7.txt" contains:

    orannges
    lemon lemon
    pickles hammburger
    bagel

    This compiles and I get:

    001 orannges
    002 lemon lemon
    003 pickles hammburger
    004 bagel

    I thought the ".+" would only match strings with one or more repeating characters. I expected:

    001 orannges

    What did I do wrong?

    Thanks!
    Beth
    Last edited by eWish; Nov 29 '07, 11:16 PM. Reason: Added Code Tags
  • rellaboyina
    New Member
    • Jan 2007
    • 55

    #2
    Originally posted by BethL
    Hello,

    I'm new to Perl and I'm stepping through an online tutorial. I'm trying to count the lines in a file that have strings with double letters in them. This is my code -

    [CODE=perl]$file = 'c:\Perl\Beth\f 7.txt';
    open(INFO, $file);
    $i = 1;
    while ($line = <INFO>)
    {
    $_ = $line;
    if (/.+/)
    {
    print "00$i $line";
    $i++;
    }
    }
    close(INFO);[/CODE]
    "f7.txt" contains:

    orannges
    lemon lemon
    pickles hammburger
    bagel

    This compiles and I get:

    001 orannges
    002 lemon lemon
    003 pickles hammburger
    004 bagel

    I thought the ".+" would only match strings with one or more repeating characters. I expected:

    001 orannges

    What did I do wrong?

    Thanks!
    Beth

    If my guess is not wrong, the output you want is :

    001 orannges
    003 pickles hammburger

    Is it right?

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      .+ would match one or more of any character in the lines. It will not match repeated characters that are the same. For that you would use:

      /(.)\1/

      or if you only want to count repeated word characters

      /(\w)\1/

      post a link to the online tutorial you are using.

      Comment

      • jonathan184
        New Member
        • Nov 2006
        • 154

        #4
        Thats right ............... ............... ..........

        Comment

        • vijaybabudev81
          New Member
          • Oct 2007
          • 6

          #5
          Plz try this...

          [code=perl]
          $file = 'c:\Perl\Beth\f 7.txt';
          open(INFO, $file);
          while ($line = <INFO>)
          {
          while($line =~m{(\w)}igs)
          {
          $a = $1;
          if($line =~m{$a{2}}is)
          {
          print "$a-$line\n";
          }
          }
          }
          close(INFO);
          [/code]
          Last edited by numberwhun; Dec 4 '07, 01:17 PM. Reason: add code tags

          Comment

          • vijaybabudev81
            New Member
            • Oct 2007
            • 6

            #6
            Sorry. try this....
            [code=perl]
            $file = 'c:\Perl\Beth\f 7.txt';
            open(INFO, $file);
            while ($line = <INFO>)
            {
            while($line =~m{(\w)}igs)
            {
            $a = $1;
            if($line =~m{$a{2}}is)
            {
            print "$a-$line\n";
            last;
            }
            }
            }
            close(INFO);
            [/code]
            Last edited by numberwhun; Dec 4 '07, 01:17 PM. Reason: add code tags

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by vijaybabudev81
              Sorry. try this....
              [code=perl]
              $file = 'c:\Perl\Beth\f 7.txt';
              open(INFO, $file);
              while ($line = <INFO>)
              {
              while($line =~m{(\w)}igs)
              {
              $a = $1;
              if($line =~m{$a{2}}is)
              {
              print "$a-$line\n";
              last;
              }
              }
              }
              close(INFO);
              [/code]
              I understand that you are trying to be helpful, but that is two posts in a row without the proper code tags.

              Please be sure and use them next time!

              Regards,

              Jeff

              Comment

              • rellaboyina
                New Member
                • Jan 2007
                • 55

                #8
                You can try this one too..

                Code:
                $file = 'c:\Perl\Beth\f7.txt'
                open(INFO, $file);
                while ($_ = <INFO>)
                {
                    $line = $_;
                    $line =~ s/(\d+)(\w+)/$2/g;
                    if($line =~ /(.)\1/)
                    {
                      print $line;
                    }
                   }
                close(INFO);

                Comment

                • archulu
                  New Member
                  • Mar 2007
                  • 34

                  #9
                  hello friend
                  plz write ur problem properly, with neat example.(not ur code)

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by archulu
                    hello friend
                    plz write ur problem properly, with neat example.(not ur code)
                    First, please refrain from the SMS speak. You can expand plz to 'please' and 'ur' to your. We are not limited to the 160 character SMS limit here in the forums.

                    Second, why should the user NOT post their code? We go through painstaking measures and repeated posts requesting the users code because we need to see it. We also like to have a sample of any data they are parsing. Any information that the user can provide (errors, code, data, etc) is always helpful, especially when we are trying to troubleshoot an issue. Please do not go telling an OP not to post their code, as we always ask them to.

                    Regards,

                    Jeff

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Originally posted by numberwhun
                      First, please refrain from the SMS speak. You can expand plz to 'please' and 'ur' to your. We are not limited to the 160 character SMS limit here in the forums.

                      Second, why should the user NOT post their code? We go through painstaking measures and repeated posts requesting the users code because we need to see it. We also like to have a sample of any data they are parsing. Any information that the user can provide (errors, code, data, etc) is always helpful, especially when we are trying to troubleshoot an issue. Please do not go telling an OP not to post their code, as we always ask them to.

                      Regards,

                      Jeff
                      I think it is important to note here that we ask OPs to post their relevant code not their entire code. But if their entire code consists of less then 20 lines then posting the whole code is not an issue.

                      It is inappropriate for users to go around willy-nilly and tell other users to not post their entire code but just examples, that is the moderators job. You don't need to do that.

                      Further, I take a very hard line on words like "plz" and "ur". You get only one warning from me, and then if I see it again, the user who does it will be met with severe consequences.

                      -MODERATOR

                      Comment

                      Working...