Help on regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    Help on regex

    Hi to all,

    I have user registry I want list out all user register by date range.For that i am using grep to filter the records but the regex is not working.I know i build wrong regex could u please anybody suggest me how do i do that.





    [CODE=perl]#!/usr/bin/perl

    my $cUser_Name="ra jiv101";

    $cYear1=2008;
    $cYear2=2008;
    $cMon1=02;
    $cMon2=02;
    $cDay2=18;
    $cDay1=18;


    my @data=<DATA>;

    my @file=grep(/$cUser_Name#[$cYear1-$cYear2]-[$cMon1-$cMon2]-[$cDay1-$cDay2]/,@data);

    print @file;


    __END__
    rajiv101#2008-02-16#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-19#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y[/CODE]



    How do i specify the date range in regexp.The character class [] is not use to specify the range which is more than one char.

    Is any idea.

    Thank u
    Rajiv.
  • rellaboyina
    New Member
    • Jan 2007
    • 55

    #2
    Originally posted by rajiv07
    Hi to all,

    I have user registry I want list out all user register by date range.For that i am using grep to filter the records but the regex is not working.I know i build wrong regex could u please anybody suggest me how do i do that.





    [CODE=perl]#!/usr/bin/perl

    my $cUser_Name="ra jiv101";

    $cYear1=2008;
    $cYear2=2008;
    $cMon1=02;
    $cMon2=02;
    $cDay2=18;
    $cDay1=18;


    my @data=<DATA>;

    my @file=grep(/$cUser_Name#[$cYear1-$cYear2]-[$cMon1-$cMon2]-[$cDay1-$cDay2]/,@data);

    print @file;


    __END__
    rajiv101#2008-02-16#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y
    rajiv101#2008-02-19#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#127.0.0.1#C Y#Y[/CODE]



    How do i specify the date range in regexp.The character class [] is not use to specify the range which is more than one char.

    Is any idea.

    Thank u
    Rajiv.


    Dear Rajiv,

    As per my understanding, here is what you want:

    You have a registry file whose data was stored based on the username and the acess dates. You want to get the data from the file which matches with the reg-ex given by you.

    Did you want to get the data along with the date or were you searching the data with the username itself?

    Please correct me if I am wrong.

    Comment

    • rajiv07
      New Member
      • Jun 2007
      • 141

      #3
      Originally posted by rellaboyina
      Dear Rajiv,

      As per my understanding, here is what you want:

      You have a registry file whose data was stored based on the username and the acess dates. You want to get the data from the file which matches with the reg-ex given by you.

      Did you want to get the data along with the date or were you searching the data with the username itself?

      Please correct me if I am wrong.
      Thanks rellaboyina

      Let me Explain clearly , Actually i am search using both username and date range.Suppose the user name 'rajiv101' and the date range '2008-12-01' to '2008-12-05' then it should list all records with this condition.

      But my Regex is totally wrong.Now i am working on that part but if u have any idea please let me know.

      Thank You
      Rajiv

      Comment

      • rajiv07
        New Member
        • Jun 2007
        • 141

        #4
        I am finding hard to solve this problem.If anybody have any other solution please let me know.

        Thank U.
        Rajiv

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by rajiv07
          I am finding hard to solve this problem.If anybody have any other solution please let me know.

          Thank U.
          Rajiv
          The [ ] match any of the characters within. If you specify something like [11-15], then it would match numbers 11 through 15. Does that do the range you are looking for?

          Regards,

          Jeff

          Comment

          • rajiv07
            New Member
            • Jun 2007
            • 141

            #6
            Originally posted by numberwhun
            The [ ] match any of the characters within. If you specify something like [11-15], then it would match numbers 11 through 15. Does that do the range you are looking for?

            Regards,

            Jeff
            Thanks Jeff

            This is what i am expecting.But if i run my above code it gives some error

            Invalid [] range "8-2" in regex; marked by <-- HERE in m/rajiv101#[2008-2 <-- HERE 008]-[2-2]-[18-18]/ at



            It is integer range but gives the error.please suggest my mistake.

            Thank U.
            Rajiv

            Comment

            • rajiv07
              New Member
              • Jun 2007
              • 141

              #7
              [code=perl]
              #!/usr/bin/perl
              while(<DATA>)
              {
              print "Match" if ~/[20-29]/;
              }
              [/code]
              __END__
              1

              Its printing 'Match' But how it is possible.Could anybody Explain Me Please.

              Thanks
              RajivGandhi
              Last edited by numberwhun; Feb 18 '08, 01:52 PM. Reason: add code tags

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Originally posted by rajiv07
                [code=perl]
                #!/usr/bin/perl
                while(<DATA>)
                {
                print "Match" if ~/[20-29]/;
                }
                [/code]
                __END__
                1

                Its printing 'Match' But how it is possible.Could anybody Explain Me Please.

                Thanks
                RajivGandhi
                Because you needed to give a valid range, I would assume. Ranges go from lowest to highest.

                Also, as I have mentioned to you in the past, please remember to put the code tags around your code.

                Regards,

                Jeff

                Comment

                • rajiv07
                  New Member
                  • Jun 2007
                  • 141

                  #9
                  Originally posted by numberwhun
                  Because you needed to give a valid range, I would assume. Ranges go from lowest to highest.

                  Also, as I have mentioned to you in the past, please remember to put the code tags around your code.

                  Regards,

                  Jeff
                  Sorry Jeff i forgot to put the code tag,

                  But the value 1 is not in the integer range [20-29] then how can it print Match.


                  And

                  [CODE=perl]#!/usr/bin/perl
                  while(<DATA>)
                  {
                  print "Match" if ~/[19-21]/;
                  }
                  __END__
                  1[/CODE]

                  If i run the above code it gives some error.Please explain me.
                  The Error is

                  Invalid [] range "9-2" in regex; marked by <-- HERE in m/[19-2 <-- HERE 1]/ at C:\DOCUME~1\IND IAM~1.COM\LOCAL S~1\Temp\loc604 .tmp line 4.

                  But [19-21] is a valid range if i am right, i am getting confuse.Please explain me.

                  Thanks
                  Rajiv.

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by rajiv07
                    Sorry Jeff i forgot to put the code tag,

                    But the value 1 is not in the integer range [20-29] then how can it print Match.


                    And

                    [CODE=perl]#!/usr/bin/perl
                    while(<DATA>)
                    {
                    print "Match" if ~/[19-21]/;
                    }
                    __END__
                    1[/CODE]

                    If i run the above code it gives some error.Please explain me.
                    The Error is

                    Invalid [] range "9-2" in regex; marked by <-- HERE in m/[19-2 <-- HERE 1]/ at C:\DOCUME~1\IND IAM~1.COM\LOCAL S~1\Temp\loc604 .tmp line 4.

                    But [19-21] is a valid range if i am right, i am getting confuse.Please explain me.

                    Thanks
                    Rajiv.
                    I think it has to be a single digit range. Sorry. Try this:

                    [code=perl]
                    print "Match" if ~/[1-2][0-1|9]/;
                    [/code]

                    I don't know if that will match exactly. I know it matches more than what you want. Hopefully someone knows a more specific way to do the range you are looking for. I will look around some more when I get a chance.

                    Regards,

                    Jeff

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      inside the character each individual character is a single character, you can't use double-digits to represent one number, like 12.

                      [19-21]

                      the above is interpreted as: 1 9-2 1 (one nine thru two one)

                      it is not interpreted as: 19-21 (nineteen thru twentyone)

                      So the invalid range of 9-2 is an error.

                      Comment

                      • Kelicula
                        Recognized Expert New Member
                        • Jul 2007
                        • 176

                        #12
                        Originally posted by KevinADC
                        inside the character each individual character is a single character, you can't use double-digits to represent one number, like 12.

                        [19-21]

                        the above is interpreted as: 1 9-2 1 (one nine thru two one)

                        it is not interpreted as: 19-21 (nineteen thru twentyone)

                        So the invalid range of 9-2 is an error.
                        Exactly.


                        Remember EVERY character within a character class becomes 1 character...
                        So /[abba dabba]/
                        is the same as: /[abd]/

                        Understand??

                        When testing numericals I personally wouldn't use a regex.

                        Try:
                        [code=perl]


                        while(<DATA>)

                        {

                        print "Match" if (($_ >19 )&& ($_ < 21));

                        }

                        __END__

                        1
                        [/code]

                        Of course that's just because I don't know how.

                        Hope it helps...
                        Last edited by Kelicula; Feb 19 '08, 01:16 AM. Reason: change numbers to match last post

                        Comment

                        • rajiv07
                          New Member
                          • Jun 2007
                          • 141

                          #13
                          Thanks to all My Master's

                          Now i got the point.I will ask u if i have any further doubt on this.

                          Regards

                          Rajiv

                          Comment

                          • rajiv07
                            New Member
                            • Jun 2007
                            • 141

                            #14
                            [CODE=perl]#!/usr/bin/perl
                            my $cUser_Name="ra jiv101";
                            my @file;

                            my $cYear1=2008;

                            my $cYear2=2008;

                            my $cMon1=02;

                            my $cMon2=02;

                            my $cDay2=18;

                            my $cDay1=17;

                            while(<DATA>)
                            {
                            my $date=(split(/#/,$_,3))[1];

                            my ($year,$mon,$da y)=split(/\-/,$date);

                            if((($year >= $cYear1)&&($yea r<=$cYear2))&&( ($mon >= $cMon1)&&($mon< =$cMon2))&&(($d ay >= $cDay1)&&($day< =$cDay2))){
                            push(@file,$_);
                            }
                            }

                            print @file;





                            __END__

                            rajiv101#2008-02-16#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y

                            rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y

                            rajiv101#2008-02-17#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y

                            rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y

                            rajiv101#2008-02-18#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y

                            rajiv101#2008-02-19#10:06:00#19: 49:33#127.0.0.1 #19:49:54#18:18 :00#1 27.0.0.1#CY#Y[/CODE]

                            Please suggest me that the above code is better way to list out the records between the date range.

                            Thanks
                            Rajiv

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              The way you are doing it is OK, but are you sure you need to check the year, month, and day?

                              Comment

                              Working...