473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help on regex

rajiv07
141 100+
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
16 2687
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
141 100+
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
141 100+
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
3,509 Expert Mod 2GB
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
141 100+
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
141 100+
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
3,509 Expert Mod 2GB
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
141 100+
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
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
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
176 Expert 100+
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
141 100+
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
141 100+
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
4,059 Expert 2GB
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
141 100+
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
4,059 Expert 2GB
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

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

Similar topics

6
by: Tony C | last post by:
I'm writing a python program which uses regular expressions, but I'm totally new to regexps. I've got Kuchling's "Regexp HOWTO", "Mastering Regular Expresions" by Oreilly, and have access to...
8
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a...
4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
4
by: henrik | last post by:
Hi I have a regex question. I want to find all content of a <td class="someclass"> tag. This means the expression should include all other tags included between <td class="someclass"> and </td>....
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
3
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a...
10
by: supercrossking | last post by:
I am trying to the values of string of text in the sample before. The ds are for digits and s is for string and string of text is for a string with more than one or two values. I am trying to use...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.