exact pattern matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamalatanvi
    New Member
    • Oct 2007
    • 7

    exact pattern matching

    hi,
    i am very new to perl

    how to give exact pattern matching
    example:
    i have the data as follows

    mm100
    mm100-m1
    mm101
    mm101-m1
    mm101-m2


    so, i should get only
    mm100
    mm101

    or
    mm100-m1
    mm101-m1
    mm101-m2


    if i say =~/mm-/
    i am retreiveing all the values
    Give me the solution pls.....
  • mehj123
    New Member
    • Aug 2007
    • 55

    #2
    Originally posted by kamalatanvi
    hi,
    i am very new to perl

    how to give exact pattern matching
    example:
    i have the data as follows

    mm100
    mm100-m1
    mm101
    mm101-m1
    mm101-m2


    so, i should get only
    mm100
    mm101

    or
    mm100-m1
    mm101-m1
    mm101-m2


    if i say =~/mm-/
    i am retreiveing all the values
    Give me the solution pls.....
    If you are making an exact comparison you can use '^' at the beginning and '$' at end...
    e.g.
    Code:
    $var =~ /^mm$/
    '^' stands for beginning and '$' stands for end of the string..

    But one doubt. If you want to do an exact comparison y cant you go simply for

    Code:
    $var eq 'mm'

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by kamalatanvi
      hi,
      i am very new to perl

      how to give exact pattern matching
      example:
      i have the data as follows

      mm100
      mm100-m1
      mm101
      mm101-m1
      mm101-m2


      so, i should get only
      mm100
      mm101

      or
      mm100-m1
      mm101-m1
      mm101-m2


      if i say =~/mm-/
      i am retreiveing all the values
      Give me the solution pls.....
      You will definitely want to read up a little bit on your regular expressions as this is not really that difficult. To get just the mm100(or 101) minus the "-m1" or "-m2" try the following:

      [code-perl]
      $var =~ m/^(\w{2}\d{3})\-\w\d$/
      [/code]

      The encolsing ( ) around the first half of that can be referenced now as $1, which will contain that match.

      Regards,

      Jeff

      Comment

      Working...