reg exp
am 25.05.2011 16:39:28 von Irfan Sayed
--0-801615448-1306334368=:40170
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
hi,
i have string like this=0A"2011/05/25 07:24:58 -0700 PDT"=A0 i nee=
d to match "2011/05/25"=0Ai wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ b=
ut it is not working
code is like this
=0A$lin =3D "2011/05/25 =
07:24:58 -0700 PDT";=0A$lin =3D~ m/^\d\d\d\d//\d\d/\d\d$/;=0Aprint "$lin\n"=
;
plz suggest =0A
--0-801615448-1306334368=:40170--
RE: reg exp
am 25.05.2011 16:52:05 von Marco van Kammen
Surely not perfect but this seems to work...
$lin =3D "2011/05/25 07:24:58 -0700 PDT";
$lin =3D~ /(^\d+\/\d+\/\d+).*/;
print "$1\n";
Marco van Kammen
Applicatiebeheerder
Mirabeau | Managed Services Dr. C.J.K. van Aalstweg 8F 301, 1625 NV Hoor=
n
+31(0)20-5950550 - www.mirabeau.nl
Please consider the environment before printing this email-----Original Me=
ssage-----
From: Irfan Sayed [mailto:irfan_sayed2002@yahoo.com]
Sent: Wednesday, May 25, 2011 4:39 PM
To: Perl Beginners
Subject: reg exp
hi,
i have string like this
"2011/05/25 07:24:58 -0700 PDT" i need to match "2011/05/25"
i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working
code is like this
$lin =3D "2011/05/25 07:24:58 -0700 PDT";
$lin =3D~ m/^\d\d\d\d//\d\d/\d\d$/;
print "$lin\n";
plz suggest
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 25.05.2011 17:06:14 von Irfan Sayed
--0-124958919-1306335974=:52522
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
thanks it worked
________________________________=0AFrom: Marco =
van Kammen =0ATo: Irfan Sayed
o.com>; Perl Beginners =0ASent: Wednesday, May 25, 2011=
8:22 PM=0ASubject: RE: reg exp
Surely not perfect but this seems to =
work...
$lin =3D "2011/05/25 07:24:58 -0700 PDT";=0A$lin =3D~ /(^\d+\/=
\d+\/\d+).*/;=0Aprint "$1\n";
Marco van Kammen=0AApplicatiebehee=
rder
=0AMirabeau | Managed Services=A0 =A0 Dr. C.J.K. van Aalstw=
eg 8F 301, 1625 NV Hoorn=0A+31(0)20-5950550=A0 -=A0 www.mirabeau.nl
Pl=
ease consider the environment before printing this email-----Original Messa=
ge-----=0AFrom: Irfan Sayed [mailto:irfan_sayed2002@yahoo.com]=0ASent: Wedn=
esday, May 25, 2011 4:39 PM=0ATo: Perl Beginners=0ASubject: reg exp
hi=
,
i have string like this=0A"2011/05/25 07:24:58 -0700 PDT"=A0 i need =
to match "2011/05/25"=0Ai wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but=
it is not working
code is like this
=0A$lin =3D "2011/05/25 07:2=
4:58 -0700 PDT";=0A$lin =3D~ m/^\d\d\d\d//\d\d/\d\d$/;=0Aprint "$lin\n";=0A=
=0Aplz suggest
--0-124958919-1306335974=:52522--
Re: reg exp
am 25.05.2011 17:09:43 von Jim Gibson
At 7:39 AM -0700 5/25/11, Irfan Sayed wrote:
hi,
i have string like this
"2011/05/25 07:24:58 -0700 PDT" i need
to match "2011/05/25"
i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$
but it is not working
code is like this
$lin = "2011/05/25
07:24:58 -0700 PDT";
$lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
You have two forward slashes there instead of one between the year
and month fields. You will have to escape the forward slash with a
backward slash if you use forward slash to delimit your regular
expression.
If you use a different delimiter, you don't have to escape the
slashes. Using extended regular expressions helps readability as well:
$lin =~ m{ \A (\d{4} / \d{2} / \d{2}) }x;
--
Jim Gibson
Jim@Gibson.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 25.05.2011 18:13:00 von Shawn H Corey
On 11-05-25 10:39 AM, Irfan Sayed wrote:
> hi,
>
> i have string like this
> "2011/05/25 07:24:58 -0700 PDT" i need to match "2011/05/25"
> i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working
>
> code is like this
>
>
> $lin = "2011/05/25 07:24:58 -0700 PDT";
> $lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
> print "$lin\n";
>
> plz suggest
>
They are two slashes in a row; change the first to a backslash:
m/^\d\d\d\d\/\d\d/\d\d$/;
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 25.05.2011 18:38:54 von Rob Dixon
On 25/05/2011 15:39, Irfan Sayed wrote:
> hi,
>
> i have string like this
> "2011/05/25 07:24:58 -0700 PDT" i need to match "2011/05/25"
> i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working
>
> code is like this
>
>
> $lin = "2011/05/25 07:24:58 -0700 PDT";
> $lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
> print "$lin\n";
>
> plz suggest
Please always write your code with 'strict' and 'warnings' in place,
especially before seeking help elsewhere. This program
use strict;
use warnings;
my $lin = "2011/05/25 07:24:58 -0700 PDT";
$lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
produces the following errors:
Backslash found where operator expected at E:\Perl\source\ps.pl line 5, near "d\"
Backslash found where operator expected at E:\Perl\source\ps.pl line 5, near "d\"
syntax error at E:\Perl\source\ps.pl line 5, near "d\"
Execution of E:\Perl\source\ps.pl aborted due to compilation errors.
And tells you immediately that there is something wrong with your regex.
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 25.05.2011 23:10:53 von rvtol+usenet
On 2011-05-25 16:39, Irfan Sayed wrote:
> i have string like this
> "2011/05/25 07:24:58 -0700 PDT" i need to match "2011/05/25"
> i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working
>
> code is like this
>
>
> $lin = "2011/05/25 07:24:58 -0700 PDT";
> $lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
> print "$lin\n";
1. Do you mean matching, or capturing?
2. When there is a slash inside your data, just use a different
separator, like {} (see Jim Gibson's answer).
3. '[0-9]' is a better character class here than \d, because \d matches
over 200 code points.
4. Use strict and warnings, (see Rob Dixon's answer).
#!/usr/bin/perl -wl
use strict;
my $line = qq{2011/05/25 07:24:58 -0700 PDT\n};
my ($date, $time, $tz_offset, $tz_abbrev ) =
$line =~ m{\A
( [0-9]{4}/[0-9]{2}/[0-9]{2} ) # date
\s+
( [0-9]{2}:[0-9]{2}:[0-9]{2} ) # time
\s+
( [-+][0-9]{4} ) # tz-offset
\s+
( [A-Z]{3} )
\Z}x;
print sprintf q{date='%s' time='%s' offs='%s' abbr='%s'},
$date, $time, $tz_offset, $tz_abbrev;
__END__
Alternative-1:
my ($date, $time, $tz_offset, $tz_abbrev ) = split ' ', $line;
Alternative-2:
my %datim;
@datim{qw/ date time tz_offset tz_abbrev /} = split ' ', $line;
Alternative-3:
my %datim;
@datim{qw/ date time tz_offset tz_abbrev /} = $line =~ /\S+/g;
Alternative-4:
Use a module that knows about dates and times.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 26.05.2011 04:41:35 von Abhinav Kukreja
--000e0cdf4b6683840204a424c22c
Content-Type: text/plain; charset=ISO-8859-1
hi,
suppose i have a string that contains n number of date patterns in the
string.
"2011/05/25 07:24:58 -0700 PDT 2011/04/28 XXXXXXXXXXXX 2023/23/45"
how can i extract all such patterns.
On Wed, May 25, 2011 at 10:08 PM, Rob Dixon wrote:
> On 25/05/2011 15:39, Irfan Sayed wrote:
> > hi,
> >
> > i have string like this
> > "2011/05/25 07:24:58 -0700 PDT" i need to match "2011/05/25"
> > i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working
> >
> > code is like this
> >
> >
> > $lin = "2011/05/25 07:24:58 -0700 PDT";
> > $lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
> > print "$lin\n";
> >
> > plz suggest
>
> Please always write your code with 'strict' and 'warnings' in place,
> especially before seeking help elsewhere. This program
>
> use strict;
> use warnings;
>
> my $lin = "2011/05/25 07:24:58 -0700 PDT";
> $lin =~ m/^\d\d\d\d//\d\d/\d\d$/;
>
> produces the following errors:
>
> Backslash found where operator expected at E:\Perl\source\ps.pl line 5,
> near "d\"
> Backslash found where operator expected at E:\Perl\source\ps.pl line 5,
> near "d\"
> syntax error at E:\Perl\source\ps.pl line 5, near "d\"
> Execution of E:\Perl\source\ps.pl aborted due to compilation errors.
>
> And tells you immediately that there is something wrong with your regex.
>
> Cheers,
>
> Rob
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--000e0cdf4b6683840204a424c22c--
Re: reg exp
am 26.05.2011 05:03:16 von Jim Gibson
At 8:11 AM +0530 5/26/11, Abhinav Kukreja wrote:
>hi,
>
>suppose i have a string that contains n number of date patterns in the
>string.
>"2011/05/25 07:24:58 -0700 PDT 2011/04/28 XXXXXXXXXXXX 2023/23/45"
>
>how can i extract all such patterns.
Use a regular expression that matches just those strings, use the
///g modifier (global), and either use the regular expression
repeatedly in a while loop or assign the return value(s) to an array
(or otherwise use in list context).
Here is an example (untested):
while( $string =~ m{ (\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d [-+]\d\d\d\d
\w\w\w) }x ) {
# matched substring is in $1
}
Or this:
my @matches = ($string =~ m{ (\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d
[-+]\d\d\d\d \w\w\w) }x );
You can, of course, modify the regular expression and make it more or
less discriminatory (and certainly shorter), but you should get the
idea.
Good luck.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: reg exp
am 31.05.2011 11:43:49 von Chris Nehren
On Wed, May 25, 2011 at 07:39:28 -0700 , Irfan Sayed wrote:
> hi,
>=20
> i have string like this
> "2011/05/25 07:24:58 -0700 PDT"=A0 i need to match "2011/05/25"
> i wrote reg ex like this: ^\d\d\d\d//\d\d/\d\d$ but it is not working=20
>=20
> code is like this=20
>=20
>=20
> $lin =3D "2011/05/25 07:24:58 -0700 PDT";
> $lin =3D~ m/^\d\d\d\d//\d\d/\d\d$/;
> print "$lin\n";
>=20
> plz suggest=20
I suggest you look at the POSIX::strptime module and function. If you're
familiar with strptime(3), it provides that interface for parsing dates
and times. For example, you could parse your dates like so:
POSIX::strptime("2011/05/25 07:24:58 -0700 PDT", "%Y/%m/%d")
There are other formats for matching the time, the UTC timezone offset,
and the timezone name. But the example given should get you started.
Additionally, if you're doing lots of date/time manipulation, consider
DateTime.pm and DateTime::Format::Strptime, which uses strptime(3) to
create DateTime objects.
--=20
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/