Re: Regex question.

2003-07-24 Thread Dennis Stout
> my $date =~ s#(\d{2})(\d{2})(\d{4})#$1/$2/$3#;

That amazingly, doesn't have much performance loss to it.

I just did:

sub build_list_news {
my $newstext = "";
my %news = get_news();
foreach (keys %news) {
$news{$_}{ctime} =~
s#(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})#$1/$2/$3 $4:$5:$6#;
$newstext .= "$news{$_}{ctime} -
$news{$_}{subject}
$news{$_}{news}";
}
$newstext .= "";

return $newstext;
}

and I get results at pretty much the same speed I was before I added the
regex.

So, even if there is a performance loss, it's still less then the lagtime
between my workstation and the server it talks to through a 100base hub..
(which is by no means significant)


> i use #'s as delimaters here... some other character may be more appropriate

I found them to be perfect when dealing with date/time stamps.

Dennis


> At 07:49 AM 6/26/2003 -0700, Sara wrote:
> >I have a database with the following fields.
> >
> >lname fnam M
> >acct# mrmbirth Postdate  Post#   drln drfn
> > m disch
>
>DOE,JOHN,R,00037839842,207337,02151956,04072003,01980,LastName,FirstName,L,04
102003
> >
> >I have a very simple script which splits the delimiter , and shows the
> >result in the same format as in database.
> >I want to do following things using regex, but I have tried my options to
> >my level best, ::) no results yet,
> >
> >1- Remove all the leading 000 from any field like acct# = 00037839842
> >should be 37939842 and Post# should be 1980
> >
> >2- Want to format dates like birth = 02151956 should be 02/15/1956
> >
> >Any help??
> >
> >Thanks,
> >
> >Sara.
>
>
>
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Regex question.

2003-07-02 Thread Scot Robnett
Sorry, that last line was wrong...

   my $in = '07032003';
   $in =~ s/^0+/$1/ if (/(\d{2})(\d{2})(\d{4})$/);

#  my $out = qq($in/$2/$3); yuck, sorry

#  Now we can do the replacement with no problem
#  because we've restructured $in to dd/dd/

   my $out = qq($1/$2/$3) if (/(\d{2})(\d{2})(\d{4})/);



-Original Message-
From: Scot Robnett [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 02, 2003 2:03 PM
To: Paul Kraus; Sara; [EMAIL PROTECTED]
Subject: RE: Regex question.


> > 2- Want to format dates like birth = 02151956 should be 02/15/1956
> my $date = "$1/$2/$3/" if (/(\d\d)(\d\d)(\d\d\d\d)/)

# All of this is UNTESTED, please treat as such.

# More of "the same but different"

   my $date = qq($1/$2/$3) if /(\d{2})(\d{2})(\d{4})/;

# Takes into account dates like 07/03/2003, unless
# of course you've lopped off the preceding "000" type
# strings with s/^0+//;  --- for example:

   my $in = '07032003';
   my $out = qq($1/$2/$3) if /(\d{2})(\d{2})(\d{4})/;
   print $out;

# would result in printing:
# 00/00/0703

# This might work better (preserving preceding "0" on the date)

   my $in = '07032003';
   $in =~ s/^0+/$1/ if (/(\d{2})(\d{2})(\d{4})$/);
   my $out = qq($in/$2/$3);


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

RE: Regex question.

2003-07-02 Thread Scot Robnett
> > 2- Want to format dates like birth = 02151956 should be 02/15/1956
> my $date = "$1/$2/$3/" if (/(\d\d)(\d\d)(\d\d\d\d)/)

# All of this is UNTESTED, please treat as such.

# More of "the same but different"

   my $date = qq($1/$2/$3) if /(\d{2})(\d{2})(\d{4})/;

# Takes into account dates like 07/03/2003, unless
# of course you've lopped off the preceding "000" type
# strings with s/^0+//;  --- for example:

   my $in = '07032003';
   my $out = qq($1/$2/$3) if /(\d{2})(\d{2})(\d{4})/;
   print $out;

# would result in printing:
# 00/00/0703

# This might work better (preserving preceding "0" on the date)

   my $in = '07032003';
   $in =~ s/^0+/$1/ if (/(\d{2})(\d{2})(\d{4})$/);
   my $out = qq($in/$2/$3);


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Regex question.

2003-07-02 Thread Paul Kraus

> 1- Remove all the leading 000 from any field like acct# = 00037839842
> should be 37939842 and Post# should be 1980
s/^0+//;

>
> 2- Want to format dates like birth = 02151956 should be 02/15/1956
my $date = "$1/$2/$3/" if (/(\d\d)(\d\d)(\d\d\d\d)/)
 


HTH
Paul Kraus

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Regex question.

2003-07-01 Thread Jon Hogue
1-
my $number =~ s/^0*(\d+)/$1/
that should trim the leading 0's

2-
my $date =~ s#(\d{2})(\d{2})(\d{4})#$1/$2/$3#;
i use #'s as delimaters here... some other character may be more appropriate

At 07:49 AM 6/26/2003 -0700, Sara wrote:
I have a database with the following fields.

lname fnam M 
acct# mrmbirth Postdate  Post#   drln drfn 
m disch
DOE,JOHN,R,00037839842,207337,02151956,04072003,01980,LastName,FirstName,L,04102003

I have a very simple script which splits the delimiter , and shows the 
result in the same format as in database.
I want to do following things using regex, but I have tried my options to 
my level best, ::) no results yet,

1- Remove all the leading 000 from any field like acct# = 00037839842 
should be 37939842 and Post# should be 1980

2- Want to format dates like birth = 02151956 should be 02/15/1956

Any help??

Thanks,

Sara.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Regex question

2002-03-11 Thread Randal L. Schwartz

> "Scot" == Scot Robnett <[EMAIL PROTECTED]> writes:

Scot> Hey y'all, I got over my brain cramp and thought I'd share with the group in
Scot> case it helps anyone trying to do something similar. I was making it way too
Scot> complicated. All I needed was:

Scot> if($email !~ /\w+@\w+\.\w{2,4}/)
Scot> {
Scot>  # error stuff here
Scot> }

And this is wrong, for many meanings of the word "wrong".

Please do not use this form of a regex.  Please.  Don't.


-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regex question

2002-03-11 Thread fliptop

Scot Robnett wrote:

> I don't think you can check for the existence of an e-mail address without
> actually attempting to send mail to it. You can ping or traceroute a domain,
> but only the mail server associated with it knows if the username is valid
> or not. If this is wrong, somebody with information please reply to the
> list - it might be on the 'wish list' for a lot of people, including me. :)


you can read more about this by typing:

perldoc -q 'valid mail address'


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Regex question

2002-03-10 Thread Scot Robnett

I don't think you can check for the existence of an e-mail address without
actually attempting to send mail to it. You can ping or traceroute a domain,
but only the mail server associated with it knows if the username is valid
or not. If this is wrong, somebody with information please reply to the
list - it might be on the 'wish list' for a lot of people, including me. :)

Scot Robnett
inSite Internet Solutions
[EMAIL PROTECTED]
www.insiteful.tv






Is there also a module or another way to test the existence of an email
address ?


Kind regards,
Rene Verharen

Please DO NOT reply to me personally.  I'll get my copy from the list.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.325 / Virus Database: 182 - Release Date: 2/19/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.325 / Virus Database: 182 - Release Date: 2/19/2002


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regex question

2002-03-10 Thread Rene Verharen

At 10-3-2002 09:36 -0500, fliptop wrote:

>>Hey y'all, I got over my brain cramp and thought I'd share with the group in
>>case it helps anyone trying to do something similar. I was making it way too
>>complicated. All I needed was:
>>if($email !~ /\w+@\w+\.\w{2,4}/)
>>{
>>  # error stuff here
>>}
>
>
>have you considered using email::valid?
>
>http://search.cpan.org/search?dist=Email-Valid

Is there also a module or another way to test the existence of an email 
address ?



Kind regards,



Rene Verharen


Please DO NOT reply to me personally.  I'll get my copy from the list.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regex question

2002-03-10 Thread fliptop

Scot Robnett wrote:

> Hey y'all, I got over my brain cramp and thought I'd share with the group in
> case it helps anyone trying to do something similar. I was making it way too
> complicated. All I needed was:
> 
> if($email !~ /\w+@\w+\.\w{2,4}/)
> {
>  # error stuff here
> }


have you considered using email::valid?

http://search.cpan.org/search?dist=Email-Valid


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regex question

2002-03-09 Thread Michael Kelly

On 3/9/02 1:46 PM, Scot Robnett <[EMAIL PROTECTED]> wrote:

Hi Scot,

> I'm trying to do a simple verification of an e-mail address format. I want
> to require:
> 
> - 1 or more alphanumeric characters
> - followed by "@"
> - followed by 1 or more alphanumerics
> - followed by a dot
> - followed by 2-4 alphanumerics.
> (for example, .tv, .com, .info)

What about things like ".co.uk"? And what about things like
<[EMAIL PROTECTED]>? I'm not that well-versed in what makes
an email address valid, but I'm sure more than a few valid email addresses
will be rejected by the above requirements.

> If the string doesn't fit these requirements, then I send them back to the
> form to re-enter the address. The problem is that using the code below, the
> program complains even when the address *is* formatted properly. Any ideas
> how I could reformat the expression better?

Yes.

> ##
> 
> if(($email !=~ /\w+[@]\w+\.\w{2}/) or ($email !=~ /\w+[@]\w+\.\w{3}/)
> or ($email !=~ /\w+[@]\w+\.\w{4}/))
> {
> print "Improperly formatted e-mail address! Please use your browser\'s back
> ";
> print "button and make sure the e-mail address is entered correctly. ";
> }
> 
> ##

First off, I'd recommend reading through the "pattern matching" section of
the Camel Book (Programming Perl, by Larry Wall, et al, published by
O'Reilly) again. There's a lot to learn.

Now, a slightly better-working version of the code above:

#   CODE   #

unless($email =~ /^\w+(\.\w+)*\@\w+(\.\w+)*\.\w{2,4}\z/){
# error stuff
}
else{
# email-ok stuff
}

# END CODE #

to examine that regex:

^\w+(\.\w+)*\@\w+(\.\w+)*\.\w{2,4}\z

^= the beginning of the string
\w+  = one or more word chars
(\.\w+)* = any number ("any number" includes zero) of sets of a dot and one
or more word chars
\@   = @ sign
\w+  = one or more word chars
(\.\w+)* = one or more word chars followed by any number of sets of a dot
and one or more word chars
\.   = a dot
\w{2,4}  = one or more word chars
\z   = the end of the string

I am NOT, however, saying that this regex necessarily matches all valid
email addresses, or that it screens out all invalid ones. It doesn't. And
nothing prevents users from entering something like "[EMAIL PROTECTED]".

hth, 

-- 
Michael


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Regex question

2002-03-09 Thread Scot Robnett

Hey y'all, I got over my brain cramp and thought I'd share with the group in
case it helps anyone trying to do something similar. I was making it way too
complicated. All I needed was:

if($email !~ /\w+@\w+\.\w{2,4}/)
{
 # error stuff here
}


-
Scot Robnett
inSite Internet Solutions
[EMAIL PROTECTED]
http://www.insiteful.tv


-Original Message-
From: Scot Robnett [mailto:[EMAIL PROTECTED]]
Sent: Saturday, March 09, 2002 3:47 PM
To: [EMAIL PROTECTED]
Subject: Regex question


I'm trying to do a simple verification of an e-mail address format. I want
to require:

- 1 or more alphanumeric characters
- followed by "@"
- followed by 1 or more alphanumerics
- followed by a dot
- followed by 2-4 alphanumerics.
  (for example, .tv, .com, .info)

If the string doesn't fit these requirements, then I send them back to the
form to re-enter the address. The problem is that using the code below, the
program complains even when the address *is* formatted properly. Any ideas
how I could reformat the expression better?

##

if(($email !=~ /\w+[@]\w+\.\w{2}/) or ($email !=~ /\w+[@]\w+\.\w{3}/)
or ($email !=~ /\w+[@]\w+\.\w{4}/))
{
 print "Improperly formatted e-mail address! Please use your browser\'s back
";
 print "button and make sure the e-mail address is entered correctly. ";
}

##



-
Scot Robnett
inSite Internet Solutions
[EMAIL PROTECTED]
http://www.insiteful.tv

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.325 / Virus Database: 182 - Release Date: 2/19/2002


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.325 / Virus Database: 182 - Release Date: 2/19/2002

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.325 / Virus Database: 182 - Release Date: 2/19/2002


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regex question

2001-06-16 Thread Ãèªá¤ô¤ë

How you get the data? From a CGI-FORM? or STDIN?
Suppose you get the data from a string format...
I will do in this way..

$value_list =~ s/,/COMMA/eg;
@data = split(/,/, $value_list);

... expressions.
... expressions.
... expressions.

($d0_value0, d0_$value1, $d0_value2) = split(/COMMA/, $data[0]);
($d1_value0, d1_$value1, $d1_value2) = split(/COMMA/, $data[1]);

... expressions.
... expressions.
... expressions.




> > Hi all,
> >
> >  I have a line that has each field separated by commas. However, some of
> > individual fields are double quoted strings and also have embedded
commas in
> > them. for example:
> >
> > Value1,"Value2, blah,blah,blah",Value3,"Value4,blah",Value5
> >
> > When I use the split function using the split pattern of /,/ it
obviously
> > doesn't work.
> >
> > I believe what I want is to split on all commas when they are not
followed by
> > anything, then a double quote , then another comma.
> >
> > This seems like it should be a simple string to split into array values,
but I
> > just can't seem to get it to work.
> > Anybody have any suggestions?





Re: regex question

2001-06-16 Thread Hasanuddin Tamir

On Fri, 15 Jun 2001, Robert Watterson <[EMAIL PROTECTED]> wrote,

> Hi all,
>
>  I have a line that has each field separated by commas. However, some of
> individual fields are double quoted strings and also have embedded commas in
> them. for example:
>
> Value1,"Value2, blah,blah,blah",Value3,"Value4,blah",Value5
>
> When I use the split function using the split pattern of /,/ it obviously
> doesn't work.
>
> I believe what I want is to split on all commas when they are not followed by
> anything, then a double quote , then another comma.
>
> This seems like it should be a simple string to split into array values, but I
> just can't seem to get it to work.
> Anybody have any suggestions?

use Text::ParseWords;

{
from perlfaq4:
How can I split a [character] delimited string except
when inside [character]? (Comma-separated files)
}

__END__
-- 
s::a::n->http(www.trabas.com)




Re: regex question

2001-06-15 Thread Timothy Kimball


Robert Watterson wrote:
:  I have a line that has each field separated by commas. However, some of
: individual fields are double quoted strings and also have embedded commas in
: them.

The Text::CSV_XS module will handle this.

-- tdk



RE: Regex question

2001-06-11 Thread Evgeny Goldin (aka Genie)


> Or better, how do I learn to use regex?

You have to read "Mastering Regular Expressions" if you wanna
be an expert, but you don't have to if all you want is to start
studying regexes. There are much simpler ways :

1) "Learning Perl" has a chapter about regular expressions
2) perldoc perlre

As about your question - the given split solution looks the good one.
I'll just let myself to correct it a bit :

> $location = "/blabla/dir/nextdir/name.txt";
> @dirs = split("\/", $location);

@dirs = split( m@[\\/]@, $location) - after Billy came up with strange
idea to use \ as a delimiter we have always to remember it

> print $dirs[$#dirs];

$dirs[ -1 ] looks better, I think ;)

Finally, File::Basename already does these things for you :

C:\>perl -w
use File::Basename;
my ( $name, $path ) = fileparse( '\blabla\dir/nextdir\name.txt' );
print $name, "\n";
print $path, "\n";

^Z
name.txt
\blabla\dir/nextdir\

C:\>





Re: Regex question

2001-06-11 Thread Curtis Poe

--- Bruno Veldeman <[EMAIL PROTECTED]> wrote:
> I have a string with this format "/blabla/dir/nextdir/name.txt"
> I want only 'name.txt' in one string and the rest in another string.

my $string = '/blabla/dir/nextdir/name.txt';
my ( $path, $file ) = ( $string=~ m!^(.*/)(.*)$! );

Breaking down the regex:

$string =~ m!^# start at beginning of string
   (  # capture to $1
 .*   #   zero or more of anything (except \n)
 /#   plus a slash
   )  # end capture
   (  # capture to $2
 .*   #   zero or more of anything
   )  # end capture
 $!x; # until end of string

Note that the \x modifier allows unescaped white space in the regex to be ignored.  
This is very
useful for commenting regexes.

In this case, however, I would recommend using File::Basename.

use File::Basename;
my $string = '/blabla/dir/nextdir/name.txt';
my $file = basename( $string );
my $path = dirname( $string );

Note that the final forward slash is not included in $path.  As a result, this does 
not *quite*
match your requirements.  Easy enough to add, though.

Cheers,
Curtis Poe


=
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/



Re: Regex question

2001-06-11 Thread Roger C Haslock

Perhaps something like

my $string = '/blabla/dir/nextdir/name.txt';

$string =~ /(.+)([^\/]*)$/;
my $everythinguptoandincludingthelastslash = $1;
my $everythingbeyondthelastslashtotheendoftheline = $2;
# [^\/] == notaslash (escaped by a backslash)
# [...]* == zero or more of them
# $/ == endofline

- Roger -


- Original Message - 
From: "Bruno Veldeman" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, June 09, 2001 9:07 PM
Subject: Regex question


> Hi,
>  
> I can't seem to get the regex right for this:
>  
> I have a string with this format "/blabla/dir/nextdir/name.txt"
> I want only 'name.txt' in one string and the rest in another string.
>  
> It would be nice explaining the regex in plain english.
>  
> Or better, how do I learn to use regex?
>  
> For now it's like chineese to me.
> (And I don't know chineese ;-) )
>  
> Thanks.
> 
> 
> 
> 




RE: Regex question

2001-06-09 Thread Moon, John

Hope this helps ...

>perl -e '$str="/blabla/dir/nextdir/name.txt";
($path,$filenm)=$str=~m/(^.+)\/(\w+\.\w+$)/;
print "path=<$path>,file name=<$filenm>\n";'

path=,file name=

John W Moon
-Original Message-
From: Bruno Veldeman [mailto:[EMAIL PROTECTED]]
Sent: June 09, 2001 16:08
To: [EMAIL PROTECTED]
Subject: Regex question


Hi,
 
I can't seem to get the regex right for this:
 
I have a string with this format "/blabla/dir/nextdir/name.txt"
I want only 'name.txt' in one string and the rest in another string.
 
It would be nice explaining the regex in plain english.
 
Or better, how do I learn to use regex?
 
For now it's like chineese to me.
(And I don't know chineese ;-) )
 
Thanks.



Re: Regex question

2001-06-09 Thread fliptop

Bruno Veldeman wrote:
> 
> I have a string with this format "/blabla/dir/nextdir/name.txt"
> I want only 'name.txt' in one string and the rest in another string.

in this case, i would use split:

$location = "/blabla/dir/nextdir/name.txt";
@dirs = split("\/", $location);

foreach (@dirs) { print "$_\n"; }

will yield:


blabla
dir
nextdir
name.txt

and you can access what you want directly by using:

print $dirs[4];

or, if you don't know how many directories will be in there:

print $dirs[$#dirs];

> Or better, how do I learn to use regex?

i'd recommend Jeffrey Friedl's book, 'Mastering Regular Expressions',
which is available from O'Reilly & Associates.