By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Help on regex

rajiv07
100+
P: 141
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.





Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $cUser_Name="rajiv101";
  4.  
  5. $cYear1=2008;
  6. $cYear2=2008;
  7. $cMon1=02;
  8. $cMon2=02;
  9. $cDay2=18;
  10. $cDay1=18;
  11.  
  12.  
  13. my @data=<DATA>;
  14.  
  15. my @file=grep(/$cUser_Name#[$cYear1-$cYear2]-[$cMon1-$cMon2]-[$cDay1-$cDay2]/,@data);
  16.  
  17. print @file;
  18.  
  19.  
  20. __END__
  21. rajiv101#2008-02-16#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  22. rajiv101#2008-02-17#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  23. rajiv101#2008-02-17#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  24. rajiv101#2008-02-18#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  25. rajiv101#2008-02-18#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  26. rajiv101#2008-02-19#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y


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.
Feb 18 '08 #1
Share this Question
Share on Google+
16 Replies


P: 55
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.





Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $cUser_Name="rajiv101";
  4.  
  5. $cYear1=2008;
  6. $cYear2=2008;
  7. $cMon1=02;
  8. $cMon2=02;
  9. $cDay2=18;
  10. $cDay1=18;
  11.  
  12.  
  13. my @data=<DATA>;
  14.  
  15. my @file=grep(/$cUser_Name#[$cYear1-$cYear2]-[$cMon1-$cMon2]-[$cDay1-$cDay2]/,@data);
  16.  
  17. print @file;
  18.  
  19.  
  20. __END__
  21. rajiv101#2008-02-16#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  22. rajiv101#2008-02-17#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  23. rajiv101#2008-02-17#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  24. rajiv101#2008-02-18#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  25. rajiv101#2008-02-18#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y
  26. rajiv101#2008-02-19#10:06:00#19:49:33#127.0.0.1#19:49:54#18:18:00#127.0.0.1#CY#Y


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.
Feb 18 '08 #2

rajiv07
100+
P: 141
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
Feb 18 '08 #3

rajiv07
100+
P: 141
I am finding hard to solve this problem.If anybody have any other solution please let me know.

Thank U.
Rajiv
Feb 18 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Feb 18 '08 #5

rajiv07
100+
P: 141
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
Feb 18 '08 #6

rajiv07
100+
P: 141
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. while(<DATA>)
  3. {
  4.  print "Match" if ~/[20-29]/;
  5. }
  6.  
__END__
1

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

Thanks
RajivGandhi
Feb 18 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. while(<DATA>)
  3. {
  4.  print "Match" if ~/[20-29]/;
  5. }
  6.  
__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
Feb 18 '08 #8

rajiv07
100+
P: 141
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. while(<DATA>)
  3. {
  4.  print "Match" if ~/[19-21]/;
  5. }
  6. __END__
  7. 1
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\INDIAM~1.COM\LOCALS~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.
Feb 18 '08 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. while(<DATA>)
  3. {
  4.  print "Match" if ~/[19-21]/;
  5. }
  6. __END__
  7. 1
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\INDIAM~1.COM\LOCALS~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:

Expand|Select|Wrap|Line Numbers
  1. print "Match" if ~/[1-2][0-1|9]/;
  2.  
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
Feb 18 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
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.
Feb 18 '08 #11

Kelicula
Expert 100+
P: 176
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. while(<DATA>)
  4.  
  5. {
  6.  
  7.  print "Match" if (($_ >19 )&& ($_ < 21));
  8.  
  9. }
  10.  
  11. __END__
  12.  
  13. 1
  14.  
Of course that's just because I don't know how.

Hope it helps...
Feb 19 '08 #12

rajiv07
100+
P: 141
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
Feb 19 '08 #13

rajiv07
100+
P: 141
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my $cUser_Name="rajiv101";
  3. my @file;  
  4.  
  5.   my $cYear1=2008;
  6.  
  7.  my $cYear2=2008;
  8.  
  9.   my $cMon1=02;
  10.  
  11.  my $cMon2=02;
  12.  
  13.   my $cDay2=18;
  14.  
  15.  my  $cDay1=17;
  16.  
  17.  while(<DATA>)
  18.  {
  19.   my $date=(split(/#/,$_,3))[1];
  20.  
  21.   my ($year,$mon,$day)=split(/\-/,$date); 
  22.  
  23.  if((($year >= $cYear1)&&($year<=$cYear2))&&(($mon >= $cMon1)&&($mon<=$cMon2))&&(($day >= $cDay1)&&($day<=$cDay2))){
  24.  push(@file,$_);
  25.  }
  26.  }
  27.  
  28. print @file;
  29.  
  30.  
  31.  
  32.  
  33.  
  34.   __END__
  35.  
  36.   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
  37.  
  38.   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
  39.  
  40.   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
  41.  
  42.   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
  43.  
  44.   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
  45.  
  46.   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
Please suggest me that the above code is better way to list out the records between the date range.

Thanks
Rajiv
Feb 19 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
The way you are doing it is OK, but are you sure you need to check the year, month, and day?
Feb 19 '08 #15

rajiv07
100+
P: 141
The way you are doing it is OK, but are you sure you need to check the year, month, and day?
Thanks Kevin

Now i have try some other method.I am using Time::Local module to find the record between the date range.Is it better way to sort this problem.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Time::Local;
  3.  
  4. my $cUser_Name="rajiv101";
  5. my @file;  
  6.  
  7.  my  $cYear1=2008;
  8.  
  9.  my  $cYear2=2008;
  10.  
  11.  my  $cMon1=2;
  12.  
  13.  my  $cMon2=2;
  14.  
  15. $cMon1-=1;
  16.  
  17. $cMon2-=1;
  18.  
  19.  my  $cDay2=18;
  20.  
  21.  my  $cDay1=18;
  22.  
  23.  my $s=0;
  24.  my $m=0;
  25.  my $h=0;
  26.  
  27.  while(<DATA>)
  28.  {
  29.  
  30.   next if /^(\s)*$/;
  31.  
  32.   my $date=(split(/#/,$_,3))[1];
  33.  
  34.  local ($year,$mon,$day)=split(/\-/,$date); 
  35.  
  36.  $mon-=1;
  37.  
  38.  if((timelocal($s,$m,$h,$day,$mon,$year) >= timelocal($s,$m,$h,$cDay1,$cMon1,$cYear1))&&(timelocal($s,$m,$h,$day,$mon,$year) <= timelocal($s,$m,$h,$cDay2,$cMon2,$cYear2)))
  39.  {
  40.   push(@file,$_) ;
  41.  }
  42.  
  43.  }
  44.  
  45. print @file;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.   __END__
  52.   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
  53.  
  54.   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
  55.  
  56.   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
  57.  
  58.   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
  59.  
  60.   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
  61.  
  62.   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
Regards
Rajiv
Feb 19 '08 #16

KevinADC
Expert 2.5K+
P: 4,059
It probably is better, but since I have no idea what you are doing with your script besides searching a range of dates it is hard to say if it is the best way to do it. Make sure to read the Time::Local documentation thoroughly as it looks like you might be making an error in calculating the month. The months are number: 0 to 11 instead of 1 to 12.
Feb 19 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.