Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression of date format

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Dear all,

I have a date format of

Tue Jun 19 10:28:17 2007

I need to be able to spot this format as a date in a variable read from a file.

I have tried with the following code, unfortunately it is not working.

Any help much appreciated.

Alf


if ( $diff_output !=~ m/(\w{3}\s+\w{3}\s+\d+\s+[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\s+\d+)/i / )

{

then do something

}
 
Your problem is in your operator "!=~". That should just be "=~". Also, you have a random trailing /

Simplifying your code a little:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$diff_output[/blue] = [red]"[/red][purple]foo Tue Jun 19 10:28:17 2007 bar[/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$diff_output[/blue] =~ [red]m/[/red][purple]([purple][b]\w[/b][/purple]{3}[purple][b]\s[/b][/purple]+[purple][b]\w[/b][/purple]{3}[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]+[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]{2}:[purple][b]\d[/b][/purple]{2}:[purple][b]\d[/b][/purple]{2}[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red]i[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Matched in '[blue]$1[/blue]'[/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
hello MillerH,

thanks for the response. Points noted. I have tried your code and still unable to get a match. I take it this worked for you !!

Cheers

Alf
 
Did you try the exact code that I provided with data and all? If that didn't print "Matched in 'Tue Jun 19 10:28:17 2007'" then I'm not going to be able to help you.

Secondly, just note that an easy way to convert that particular date format into some other format would be to use the Date::parse module (which would require installation). The following should print "Date found 2007/06/19 10:28:17".

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Date::Parse[/green] [red]qw([/red][purple]strptime[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$diff_output[/blue] = [red]"[/red][purple]foo Tue Jun 19 10:28:17 2007 bar[/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$diff_output[/blue] =~ [red]m/[/red][purple]([purple][b]\w[/b][/purple]{3}[purple][b]\s[/b][/purple]+[purple][b]\w[/b][/purple]{3}[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]+[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]{2}:[purple][b]\d[/b][/purple]{2}:[purple][b]\d[/b][/purple]{2}[purple][b]\s[/b][/purple]+[purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red]i[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Reformatted Date: [/purple][red]"[/red] . [maroon]strftime[/maroon][red]([/red][red]"[/red][purple]%Y/%m/%d %H:%M:%S[/purple][red]"[/red], [maroon]strptime[/maroon][red]([/red][blue]$1[/blue][red])[/red][red])[/red] . [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Core (perl 5.8.8) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
Other Modules used :
[ul]
[li]Date::parse[/li]
[/ul]
[/tt]

- Miller
 
hello hello MillerH,

thanks for the post. Originally, I visually copied the text onto a Unix machine. This didn't work. Then I copied the actual text from the post to a Windows box and it worked ok. I then copied this code to the Unix system and it worked ok. Strange !!!!

Thanks

Alf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top