Re: last regex question (for the evening)
On Wed, 21 Nov 2012 20:54:29 -0500 shawn wilson ag4ve...@gmail.com wrote: how do i capture all quoted strings? OK, I rewrote it as a simple parser: DATA_LOOP: while( my $line = DATA ){ print $line; my @captured = ( '' ); my $inside = 0; CAPTURE_LOOP: while( 1 ){ if( $line =~ m{ \G ( [^\\]+ | \\ [\\] ) }gcx ){ $captured[-1] .= $1 if $inside; }elsif( $line =~ m{ \G \ }gcx ){ $inside ^= 1; push @captured, '' if ! $inside; }else{ last CAPTURE_LOOP; } } pop @captured; # last one is always any empty string if( $inside ){ warn missing closing quotes\n; } print \t$_\n for @captured; print \n; } __DATA__ something 444 \escaped quote\ 321 \esc quote\ other stuff 567 Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET CLR 1.1.4322; msn OptimizedIE8;ESMX) 556 \Mozilla\ 555 -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
Oops, I found an error my parser. Here's version 1.0.2: DATA_LOOP: while( my $line = DATA ){ print $line; my @captured = ( '' ); my $inside = 0; CAPTURE_LOOP: while( 1 ){ if( $line =~ m{ \G ( [^\\]+ | \\ [\\] ) }gcx ){ $captured[-1] .= $1 if $inside; }elsif( $line =~ m{ \G \ }gcx ){ $inside ^= 1; push @captured, '' if ! $inside; }else{ last CAPTURE_LOOP; } } if( $inside ){ warn missing closing quotes\n; }else{ pop @captured; # last item is a bogus empty string } print \t$_\n for @captured; print \n; } __DATA__ something 444 \escaped quote\ 321 \esc quote\ other stuff 567 Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET CLR 1.1.4322; msn OptimizedIE8;ESMX) 556 \Mozilla\ 555 -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
i think i got it working correctly. thanks for your help: my ($ip, $ident, $userid, $time, $cgi, $url, $proto, $status, $size_tx, $ref, $ua, $size_rx, $re_time) = /^ ([-0-9\.]+)\# ip address (\S+)\ # identity (\S+)\ # userid \[([^]]+)\]\# time ([A-Z]*)[\ ]{0,1} # cgi ([^\ ]*)[\ ]{0,1} # url ([^]*)\ # proto ([-\d]+)\ # status code ([-\d]+)\ # server doc size ([^]*)\ # referring url ([^\\]*(?:\\[\\][^\\]*)*)\ # user agent ([-\d]+)\ # request size ([-\d]+)# time to process request $/x; LogFormat %h %l %u %t \%r\ %s %b \%{Referer}i\ \%{User-Agent}i\ %I %O On Thu, Nov 22, 2012 at 10:29 AM, Shawn H Corey shawnhco...@gmail.com wrote: Oops, I found an error my parser. Here's version 1.0.2: DATA_LOOP: while( my $line = DATA ){ print $line; my @captured = ( '' ); my $inside = 0; CAPTURE_LOOP: while( 1 ){ if( $line =~ m{ \G ( [^\\]+ | \\ [\\] ) }gcx ){ $captured[-1] .= $1 if $inside; }elsif( $line =~ m{ \G \ }gcx ){ $inside ^= 1; push @captured, '' if ! $inside; }else{ last CAPTURE_LOOP; } } if( $inside ){ warn missing closing quotes\n; }else{ pop @captured; # last item is a bogus empty string } print \t$_\n for @captured; print \n; } __DATA__ something 444 \escaped quote\ 321 \esc quote\ other stuff 567 Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET CLR 1.1.4322; msn OptimizedIE8;ESMX) 556 \Mozilla\ 555 -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
last regex question (for the evening)
how do i capture all quoted strings? this gives an error: print $str1\n; ($one, $two) = $str1 =~/([^(?:[^])]*) (\d+)/; print $one $two\n; what i want is: $str1 = \something\ 444; $str1 = escaped quote\\\ 321; $str1 = esc quote\\\ other stuff\ 567; to match - and sometimes there are spaces before the match, a '\' with no '', etc. i'm trying to match a useragent in an apache log file. i've gone through the source of a few modules that claim to do this and i can't figure out how they're doing it. here's a test (the later case is what's really got me btw): #!/usr/bin/perl use strict; use warnings; my $str0 = thing other; print $str0\n; my ($sane) = $str0 =~ /(\S+)(?: other)/; print $sane \n; foreach my $str((\something\ 444, escaped quote\\\ 321,esc quote\\\ other stuff\ 567, \Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET CLR 1.1.4322; msn OptimizedIE8;ESMX)\ 556, Mozilla 555 )) { print $str\n; my ($one, $two) = $str =~/([^]*) (\d+)/; print [$one] [$two]\n; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
On Wed, 21 Nov 2012 20:54:29 -0500 shawn wilson ag4ve...@gmail.com wrote: how do i capture all quoted strings? Get Regexp::Common from CPAN and read Regexp::Common::delimited. http://search.cpan.org/~abigail/Regexp-Common-2011121001/ -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
that's a pretty cool module - i especially like their regex with lookup table trick. however, since i've got a decently lengthy regex written to do what i want, and because i'm itching to know how to actually do this at this point (i'd really like to know what's wrong with the regex i posted as well), i'd sorta like a regex answer vs a module. On Wed, Nov 21, 2012 at 9:29 PM, Shawn H Corey shawnhco...@gmail.com wrote: On Wed, 21 Nov 2012 20:54:29 -0500 shawn wilson ag4ve...@gmail.com wrote: how do i capture all quoted strings? Get Regexp::Common from CPAN and read Regexp::Common::delimited. http://search.cpan.org/~abigail/Regexp-Common-2011121001/ -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
oh, and just to be clear (since i suppose it wasn't by me saying that i was looking at the regexes in other modules to accomplish this) this is, as someone else (on this list) once told me masturbation with code. i can do what i set off to accomplish but i just wanted more and it annoyed me that i couldn't figure out how to parse this with a regex. i also feel the education might be worth the masturbation time. On Wed, Nov 21, 2012 at 9:46 PM, shawn wilson ag4ve...@gmail.com wrote: that's a pretty cool module - i especially like their regex with lookup table trick. however, since i've got a decently lengthy regex written to do what i want, and because i'm itching to know how to actually do this at this point (i'd really like to know what's wrong with the regex i posted as well), i'd sorta like a regex answer vs a module. On Wed, Nov 21, 2012 at 9:29 PM, Shawn H Corey shawnhco...@gmail.com wrote: On Wed, 21 Nov 2012 20:54:29 -0500 shawn wilson ag4ve...@gmail.com wrote: how do i capture all quoted strings? Get Regexp::Common from CPAN and read Regexp::Common::delimited. http://search.cpan.org/~abigail/Regexp-Common-2011121001/ -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: last regex question (for the evening)
On Wed, 21 Nov 2012 20:54:29 -0500 shawn wilson ag4ve...@gmail.com wrote: to match - and sometimes there are spaces before the match, a '\' with no '', etc. I think you want something like: while( DATA ){ my @captured = m{ \ ( [^\\]* (?: \\ [\\] [^\\]* )* ) \ }gx; print; print \t$_\n for @captured; print \n; } __DATA__ something 444 \escaped quote\ 321 \esc quote\ other stuff 567 Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET CLR 1.1.4322; msn OptimizedIE8;ESMX) 556 \Mozilla\ 555 -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. Why fit in when you can stand out? Dr. Seuss The only way that problems get solved in real life is with a lot of hard work on getting the details right. Linus Torvalds -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
here doc/regex question
hi, i'm using the example from man perlop for here docs with indentation i'm using vim syntax with set list and set number lines 1 ^I(my $quote = '^IFINIS') =~ s/^\s+//gm;$ 2 ^IThe Road goes ever on and on,$ 3 $ 4 ^I$ 5 ^Idown from the door where it began.$ 6 ^IFINIS$ why does s/^\s+//gm kill the empty lines 3 and 4, whereas s/^\t+//gm does not? thx matthias -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: here doc/regex question
Hi Matthias, On Thu, 27 Sep 2012 12:50:56 +0200 Matthias Leopold matth...@aic.at wrote: hi, i'm using the example from man perlop for here docs with indentation i'm using vim syntax with set list and set number lines 1 ^I(my $quote = '^IFINIS') =~ s/^\s+//gm;$ 2 ^IThe Road goes ever on and on,$ 3 $ 4 ^I$ 5 ^Idown from the door where it began.$ 6 ^IFINIS$ why does s/^\s+//gm kill the empty lines 3 and 4, whereas s/^\t+//gm does not? That's because \s also matches \n (and \r). Regards, Shlomi Fish thx matthias -- - Shlomi Fish http://www.shlomifish.org/ The Case for File Swapping - http://shlom.in/file-swap Only wimps complain about bad code. Real men clean it up. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: here doc/regex question
Am 2012-09-27 13:16, schrieb Shlomi Fish: Hi Matthias, On Thu, 27 Sep 2012 12:50:56 +0200 Matthias Leopoldmatth...@aic.at wrote: hi, i'm using the example from man perlop for here docs with indentation i'm using vim syntax with set list and set number lines 1 ^I(my $quote ='^IFINIS') =~ s/^\s+//gm;$ 2 ^IThe Road goes ever on and on,$ 3 $ 4 ^I$ 5 ^Idown from the door where it began.$ 6 ^IFINIS$ why does s/^\s+//gm kill the empty lines 3 and 4, whereas s/^\t+//gm does not? That's because \s also matches \n (and \r). Regards, Shlomi Fish ok, thx. simple question, simple answer... matthias -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Shlomi Fish wrote: On Friday 02 Jul 2010 17:29:27 Dr.Ruud wrote: sdDirtySox wrote: I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); You could use the cpp, something like #define sprintf( fmt, ... ) \ sprintf_s( (fmt), sizeof(fmt), __VA_ARGS__ ) are __VA_ARGS__ and the ellipsis (...) for C preprocessor macros supported under non-GNU C implementations? Sure. (And if your current cpp can't handle it, use a different one.) -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
On Friday 02 Jul 2010 17:29:27 Dr.Ruud wrote: sdDirtySox wrote: I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); You could use the cpp, something like #define sprintf( fmt, ... ) \ sprintf_s( (fmt), sizeof(fmt), __VA_ARGS__ ) are __VA_ARGS__ and the ellipsis (...) for C preprocessor macros supported under non-GNU C implementations? Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ What does Zionism mean? - http://shlom.in/def-zionism God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
sdDirtySox wrote: I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); You could use the cpp, something like #define sprintf( fmt, ... ) \ sprintf_s( (fmt), sizeof(fmt), __VA_ARGS__ ) -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Thanks for responding. I'll give it a try. On Jul 2, 5:10 am, jwkr...@shaw.ca (John W. Krahn) wrote: sdDirtySox wrote: Hi all, Hello, I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); This may work but it is UNTESTED: s/sprintf\(\s*(\w+),\s*(.*?)\s*\);/sprintf($1, sizeof($1), $2);/ John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
regex question
Hi all, I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); Thanks in advance. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
sdDirtySox wrote: Hi all, Hello, I want to use a perl script that will look at all the files in a directory to find an instance of a function and replace it with another. I have the code in place to look at all the files in a directory and to search line by line, but I'm having problems with the regex to use. Example: sprintf(str, hello); replaced with: sprintf_s(str, sizeof(str), hello); This may work but it is UNTESTED: s/sprintf\(\s*(\w+),\s*(.*?)\s*\);/sprintf($1, sizeof($1), $2);/ John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Regex question
if ( ! defined $username || ! $username =~ /[0-9]{10,11}/ ) { do something; } else { do something else; } what that's supposed to do is this: if it's blank or not 10 or 11 digits... The question is where is my understanding faulty or did I mess up. Thanks in advance Bruce -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex question
On 24/03/10 00:13 -0700, Bruce Ferrell wrote: if ( ! defined $username || ! $username =~ /[0-9]{10,11}/ ) { do something; } else { do something else; } what that's supposed to do is this: if it's blank or not 10 or 11 digits... The question is where is my understanding faulty or did I mess up. I hope I understood you right now; I think you want the !~ operator. my $user1 = 'foobar'; my $user2 = '10100101011'; if(!defined($user1) || $user1 !~ /[0-9]{10,11}/) { print User 1 $user1 is not legal\n; } else { print $user2 is legal; } if(!defined($user2) || $user2 !~ /[0-9]{10,11}/) { print User 2 $user2 is not legal\n; } else { print $user2 is legal\n; } » perl 1.pl User 1 foobar is not legal 10100101011 is legal -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
AW: Regex question
Bruce Ferrell bferr...@baywinds.org wrote: if ( ! defined $username || ! $username =~ /[0-9]{10,11}/ ) { do something; } else { do something else; } what that's supposed to do is this: if it's blank or not 10 or 11 digits... The question is where is my understanding faulty or did I mess up. defined() is true if the checked variable has been assigned any value except undef, so defined $username will be true for an empty username, too. In any case I'd probably rather write your code like this: $username ||= ''; # make sure it's always defined unless( $username =~ m/^\d{10,11}$/ ){ # bad username } else { # good username } HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex question
Bruce Ferrell wrote: if ( ! defined $username || ! $username =~ /[0-9]{10,11}/ ) { do something; } else { do something else; } if ( $username and $username =~ /\A[0-9]{10,11}\z/ ) { ...; } else { die; } -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
regex question
Hi, I have a text file with lines like this · Experience in C/C++ realtime system programming · Experience in ACE, FIX Could anybody tell me which regex to use to get rid of the dot and the leading spaces before each Line? Thanks for any help! Jim -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
You can try following: $_ =~ s/^\.(\s)+//g; Cheers, Parag On Tue, Dec 22, 2009 at 10:59 AM, Jim Green zhang.zhengq...@gmail.comwrote: Hi, I have a text file with lines like this · Experience in C/C++ realtime system programming · Experience in ACE, FIX Could anybody tell me which regex to use to get rid of the dot and the leading spaces before each Line? Thanks for any help! Jim -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
2009/12/22 Parag Kalra paragka...@gmail.com: You can try following: $_ =~ s/^\.(\s)+//g; This isn't quite right. There are two ways in which you might use this substitution: either $_ will contain a single line, or it will contain multiple lines. The single line case might look something like this: while () { s/^\.(\s)+//g; print; } In this case, however, the /g modifier is redundant and confusing, since you only want the regex to match once at the start of each line. Take the /g modifier off. The multiple line case might look like this: $_ = do {local $/; ;}; # but better to use File::Slurp s/^\.(\s)+//g; print; This code is broken. The problem is that ^ matches start-of-string, not start-of-line. Therefore, even with the /g modifier the regex can only match at the beginning of the string, and so will only match once. To change ^ to match start-of-line, you need the /m modifier: s/^\.(\s)+//gm; print; Perl Best Practices makes the interesting recommendation that all regexes should use the /m modifier, because having ^$ match line boundaries seems to be what most programmers expect and seems to be useful much more often. If you still need to match start and end of string you can use \A and \z. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Philip Potter wrote: 2009/12/22 Parag Kalra paragka...@gmail.com: You can try following: $_ =~ s/^\.(\s)+//g; This isn't quite right. There are two ways in which you might use this substitution: either $_ will contain a single line, or it will contain multiple lines. The single line case might look something like this: while () { s/^\.(\s)+//g; print; } In this case, however, the /g modifier is redundant and confusing, since you only want the regex to match once at the start of each line. Take the /g modifier off. The multiple line case might look like this: $_ = do {local $/; ;}; # but better to use File::Slurp s/^\.(\s)+//g; print; This code is broken. The problem is that ^ matches start-of-string, not start-of-line. Therefore, even with the /g modifier the regex can only match at the beginning of the string, and so will only match once. To change ^ to match start-of-line, you need the /m modifier: s/^\.(\s)+//gm; print; Perl Best Practices makes the interesting recommendation that all regexes should use the /m modifier, because having ^$ match line boundaries seems to be what most programmers expect and seems to be useful much more often. If you still need to match start and end of string you can use \A and \z. Phil I should point out that this may not work. Text in question seems to be UTF-8. If so, this should be added before any input: use utf8; binmode ARGV, ':encoding(utf8)'; -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Thanks Philip for sharing this excellent piece of information. Cheers, Parag On Tue, Dec 22, 2009 at 8:17 PM, Philip Potter philip.g.pot...@gmail.comwrote: 2009/12/22 Parag Kalra paragka...@gmail.com: You can try following: $_ =~ s/^\.(\s)+//g; This isn't quite right. There are two ways in which you might use this substitution: either $_ will contain a single line, or it will contain multiple lines. The single line case might look something like this: while () { s/^\.(\s)+//g; print; } In this case, however, the /g modifier is redundant and confusing, since you only want the regex to match once at the start of each line. Take the /g modifier off. The multiple line case might look like this: $_ = do {local $/; ;}; # but better to use File::Slurp s/^\.(\s)+//g; print; This code is broken. The problem is that ^ matches start-of-string, not start-of-line. Therefore, even with the /g modifier the regex can only match at the beginning of the string, and so will only match once. To change ^ to match start-of-line, you need the /m modifier: s/^\.(\s)+//gm; print; Perl Best Practices makes the interesting recommendation that all regexes should use the /m modifier, because having ^$ match line boundaries seems to be what most programmers expect and seems to be useful much more often. If you still need to match start and end of string you can use \A and \z. Phil
Re: regex question
Parag Kalra wrote: You can try following: $_ =~ s/^\.(\s)+//g; You are using capturing parentheses but you are not using the results of that capture anywhere so why use them? You are using capturing parentheses in LIST context so even if there are multiple whitespace characters you are only capturing ONE. If you are using the parentheses for grouping then it would be more efficient to use non-capturing parentheses instead, but grouping only makes sense if you have a GROUP of characters to match, but you only have ONE. The use of the /g option is superfluous because the pattern is anchored to the beginning of the string and there is only ONE beginning of string in any string. John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
regex question
Hi, I have a text file with lines like this · Experience in C/C++ realtime system programming · Experience in ACE, FIX Could anybody tell me which regex to use to get rid of the dot and the leading spaces before each Line? Thanks for any help! Jim -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
JG == Jim Green student.northwest...@gmail.com writes: JG I have a text file with lines like this JG · Experience in C/C++ realtime system programming JG · Experience in ACE, FIX JG Could anybody tell me which regex to use to get rid of the dot and the JG leading spaces before each Line? what have you tried? do you have any code at all to show? this list isn't for coding for you but to help perl beginners. this is a fairly easy problem so why don't you do a basic s/// op on a line, anchor it to the beginning and try to match what you don't want and replace it with a null string. i wrote it in english so you have to translate that to perl. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
2009/12/21 Uri Guttman u...@stemsystems.com: JG == Jim Green student.northwest...@gmail.com writes: JG I have a text file with lines like this JG · Experience in C/C++ realtime system programming JG · Experience in ACE, FIX JG Could anybody tell me which regex to use to get rid of the dot and the JG leading spaces before each Line? what have you tried? do you have any code at all to show? this list isn't for coding for you but to help perl beginners. this is a fairly easy problem so why don't you do a basic s/// op on a line, anchor it to the beginning and try to match what you don't want and replace it with a null string. i wrote it in english so you have to translate that to perl. I am reading learning perl but have not proceeded to regex chapters. I googled and got to know hot to delete preceding white space. but now the problem is that there is an odd dot at the beginning... I don't know how to write the pattern in s/pattern//, so a regex for that will do. Sorry if it sounds stupid, anyway, Thanks for the help! Jim uri -- Uri Guttman -- ...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
At 11:49 PM -0600 12/21/09, Jim Green wrote: Hi, I have a text file with lines like this · Experience in C/C++ realtime system programming · Experience in ACE, FIX Could anybody tell me which regex to use to get rid of the dot and the leading spaces before each Line? s/^\.\s*//; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
2009/12/22 Jim Gibson jimsgib...@gmail.com: s/^\.\s*//; Thanks Jim, I will figure out as I read learning perl. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
On Nov 9, 2009, at 16:25, axr0284 wrote: Hi, I am completely stumped with how to proceed with this. I have a program where the user inputs a path to a file. This is under windows xp The path can be relative ..\..\..\synthesis\int_code.psm .\program\synthesis\int_code.psm or absolute c:\command\tool\program\synthesis\int_code.psm I do not know what it is in advance. 1)I would need perl to be able to distinguish between a relative and absolute path Fortunately there are lots of tools to do this in perl. :) 2) If the path is relative, create an absolute path from that. My orignal idea was to locate ..\ with regex using m/([.]{2}\\)/ This would indicate if the path is relative or not use cd to obtain the present working directory. Is there a better way to do this??? Yes; Cwd To find out more, look up Cwd in perl documentation. In short its like this: use Cwd; my $dir = getcwd; use Cwd 'abs_path'; my $abs_path = abs_path($file); Can anybody help me with this or can you think of a better idea. Avoid starting your solution with a regex if you can. Regexes tend to get complicated quickly. :) Jeremiah -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
regex question
Hi, I am completely stumped with how to proceed with this. I have a program where the user inputs a path to a file. This is under windows xp The path can be relative ..\..\..\synthesis\int_code.psm .\program\synthesis\int_code.psm or absolute c:\command\tool\program\synthesis\int_code.psm I do not know what it is in advance. 1)I would need perl to be able to distinguish between a relative and absolute path 2) If the path is relative, create an absolute path from that. My orignal idea was to locate ..\ with regex using m/([.]{2}\\)/ This would indicate if the path is relative or not use cd to obtain the present working directory. Is there a better way to do this??? then go through all the regex located and replace them appropriately with data obtained from cd. Can anybody help me with this or can you think of a better idea. Thanks a lot. Amish -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
On Mon, Nov 9, 2009 at 10:25 AM, axr0284 axr0...@yahoo.com wrote: Hi, I am completely stumped with how to proceed with this. I have a program where the user inputs a path to a file. This is under windows xp The path can be relative ..\..\..\synthesis\int_code.psm .\program\synthesis\int_code.psm or absolute c:\command\tool\program\synthesis\int_code.psm I do not know what it is in advance. 1)I would need perl to be able to distinguish between a relative and absolute path 2) If the path is relative, create an absolute path from that. My orignal idea was to locate ..\ with regex using m/([.]{2}\\)/ This would indicate if the path is relative or not use cd to obtain the present working directory. Is there a better way to do this??? Take a look at the core File::Spec module, particularly the rel2abs() method. HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom! -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
regex question
Hi I'm look for code example that will only select a string containing 3 digits .. The string cant be less than 3 digit , can't be more than 3 and can't contain any other characters. I have googled and found code that will find 3 consecutive digits within a string or at the beginning. Any ideas ? Thanks G -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
GM == Gregory Machin g...@linuxpro.co.za writes: GM I'm look for code example that will only select a string containing 3 GM digits .. The string cant be less than 3 digit , can't be more than 3 GM and can't contain any other characters. that is called containing exactly three digits. you spent way too many words describing it. GM I have googled and found code that will find 3 consecutive digits GM within a string or at the beginning. and what is that code? did you understand how they work? did you try them out? have you read any of the intros to perl regexes (perlretut and perlrequick)? finally if you have code that works at the beginning of a string, would you want to find something that matches digits at the end of a string? perl might have something useful like that so rtfm and look for it. it will be easier than you think. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
On Tue, 2009-09-15 at 09:30 +0200, Gregory Machin wrote: Hi I'm look for code example that will only select a string containing 3 digits .. The string cant be less than 3 digit , can't be more than 3 and can't contain any other characters. I have googled and found code that will find 3 consecutive digits within a string or at the beginning. Any ideas ? Thanks G So you want a regex that will identify a three digit string (any 3 digits or 3 specified digits?) and anchor to both the start and end of the string? Tim Bowden -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Any numbers, but only 3 digits in the string. I'm filtering out local extention numbers from phone numbers Where in the log the extension is 104 and the phone number would be something like PSTN0/2-0115070545. All the stuff I have tried have returned would return both the extension number and 3 digits from the phone number eg 011 . So I only want the records that beginning and end in a number and have exactly 3 digits not longer. Thanks for you time. G On Tue, Sep 15, 2009 at 9:52 AM, Tim Bowden tim.bow...@mapforge.com.au wrote: On Tue, 2009-09-15 at 09:30 +0200, Gregory Machin wrote: Hi I'm look for code example that will only select a string containing 3 digits .. The string cant be less than 3 digit , can't be more than 3 and can't contain any other characters. I have googled and found code that will find 3 consecutive digits within a string or at the beginning. Any ideas ? Thanks G So you want a regex that will identify a three digit string (any 3 digits or 3 specified digits?) and anchor to both the start and end of the string? Tim Bowden -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
On Tue, 2009-09-15 at 10:10 +0200, Gregory Machin wrote: Any numbers, but only 3 digits in the string. I'm filtering out local extention numbers from phone numbers Where in the log the extension is 104 and the phone number would be something like PSTN0/2-0115070545. All the stuff I have tried have returned would return both the extension number and 3 digits from the phone number eg 011 . So I only want the records that beginning and end in a number and have exactly 3 digits not longer. Thanks for you time. G Ah, so you do want to anchor at the beginning and end of the string. Have you looked at perlrequick (http://perldoc.perl.org/perlrequick.html) as Uri suggested? That should show you how to anchor a regex to the start and end of a string. Post what you're trying after looking there. Regards, Tim Bowden BTW, please try not to top post. It breaks the flow of the thread. On Tue, Sep 15, 2009 at 9:52 AM, Tim Bowden tim.bow...@mapforge.com.au wrote: On Tue, 2009-09-15 at 09:30 +0200, Gregory Machin wrote: Hi I'm look for code example that will only select a string containing 3 digits .. The string cant be less than 3 digit , can't be more than 3 and can't contain any other characters. I have googled and found code that will find 3 consecutive digits within a string or at the beginning. Any ideas ? Thanks G So you want a regex that will identify a three digit string (any 3 digits or 3 specified digits?) and anchor to both the start and end of the string? Tim Bowden -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
Thanks for the help .. knowing the anchor thing help me. if you not sure what ur looking for or what it call you end up with the wrong thing. On Tue, Sep 15, 2009 at 10:21 AM, Tim Bowden tim.bow...@mapforge.com.au wrote: On Tue, 2009-09-15 at 10:10 +0200, Gregory Machin wrote: Any numbers, but only 3 digits in the string. I'm filtering out local extention numbers from phone numbers Where in the log the extension is 104 and the phone number would be something like PSTN0/2-0115070545. All the stuff I have tried have returned would return both the extension number and 3 digits from the phone number eg 011 . So I only want the records that beginning and end in a number and have exactly 3 digits not longer. Thanks for you time. G Ah, so you do want to anchor at the beginning and end of the string. Have you looked at perlrequick (http://perldoc.perl.org/perlrequick.html) as Uri suggested? That should show you how to anchor a regex to the start and end of a string. Post what you're trying after looking there. Regards, Tim Bowden BTW, please try not to top post. It breaks the flow of the thread. On Tue, Sep 15, 2009 at 9:52 AM, Tim Bowden tim.bow...@mapforge.com.au wrote: On Tue, 2009-09-15 at 09:30 +0200, Gregory Machin wrote: Hi I'm look for code example that will only select a string containing 3 digits .. The string cant be less than 3 digit , can't be more than 3 and can't contain any other characters. I have googled and found code that will find 3 consecutive digits within a string or at the beginning. Any ideas ? Thanks G So you want a regex that will identify a three digit string (any 3 digits or 3 specified digits?) and anchor to both the start and end of the string? Tim Bowden -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
GM == Gregory Machin g...@linuxpro.co.za writes: GM Thanks for the help .. knowing the anchor thing help me. if you not GM sure what ur looking for or what it call you end up with the wrong GM thing. did you read my post? if you had a clue about the digits starting at the beginning of the string, that sections in the docs would also cover digits at the end of the string. also the term anchor would crop up. please learn how to read the docs and you will be a much better coder then someone who googles for every little thing. this is for your own skill improvement. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: Simple regex question
Hi Alexander Or you can use this ($temp=~ m/([A-Za-z0-9]{5})\.([0-9]{1,2})[+-]([0-9]{1,4})\.([0-9]{1,4})/); This is more compact and accurate Thanks and regards Ajay -Original Message- From: Alexander Koenig [mailto:alexander.koe...@mpi.nl] Sent: Tuesday, May 19, 2009 7:25 PM To: beginners@perl.org Subject: Re: Simple regex question You wrote on 05/19/2009 03:18 PM: Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits The best I can come up with on the fly would be something like this: --- #!/usr/bin/perl use strict; use warnings; my @test = ('A123C.11+002.001','FC32G.2-1.0','12B15.01+2145.15'); foreach my $item (@test) { print $item\n\n; ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; my ($lengthx, $lengthy) = (length $x, length $y); print a = $a, n = $n, x = $x, y = $y, length x = $lengthx, length y = $lengthy\n; } --- But the real experts can probably compress it much more still. hth Alex -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Simple regex question
Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits Some examples: A123C.11+002.001 FC32G.2-1.0 12B15.01+2145.15 I need all 4 pieces and the length of x y. The following works: my $id= A123C.11+002.001; my @tmp = split(/[\.+-]/,$id); my $part = $tmp[0]; my $unit = $tmp[1]; my $x = $tmp[2]; my $y = $tmp[3]; my $xlen = length $tmp[2]; my $ylen = length $tmp[3]; but in my quest for understanding regex, I'm always looking for more elegant (obfuscated :-) code... Anybody have a good one-liner for this? my ($part,$unit,$x,$y,$xlen,$ylen) = /* Some obfuscated code here.. */ Thanks! -Dan --- d...@mapson.ninemoons.com (To reply via email, remove 'reverse(nospam)' in address. Spambots are getting wy too smart nowadays...:-) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
You wrote on 05/19/2009 03:18 PM: Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits The best I can come up with on the fly would be something like this: --- #!/usr/bin/perl use strict; use warnings; my @test = ('A123C.11+002.001','FC32G.2-1.0','12B15.01+2145.15'); foreach my $item (@test) { print $item\n\n; ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; my ($lengthx, $lengthy) = (length $x, length $y); print a = $a, n = $n, x = $x, y = $y, length x = $lengthx, length y = $lengthy\n; } --- But the real experts can probably compress it much more still. hth Alex -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
On Tue, May 19, 2009 at 09:18, Dan Fish d...@ninemoons.com wrote: Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits snip What do you mean by chars? Is any character valid or are only printable characters valid, or only printable ASCII characters, or a-z, A-Z, and 0-9? a is (.{5}) or ([[:print:]]{5}) or ([\x{20}-\x{7e}]{5}) or ([a-zA-Z0-9]{5}) . is [.] n is ([0-9]{1,2}) x is ([+-][0-9]{1,4}) . is [.] y is ([0-9]{1,4}) Some examples: A123C.11+002.001 FC32G.2-1.0 12B15.01+2145.15 I need all 4 pieces and the length of x y. The following works: my $id= A123C.11+002.001; my @tmp = split(/[\.+-]/,$id); my $part = $tmp[0]; my $unit = $tmp[1]; my $x = $tmp[2]; my $y = $tmp[3]; my $xlen = length $tmp[2]; my $ylen = length $tmp[3]; snip my ($part, $unit, $x, $y) = / (.{5}) [.] ([0-9]{1,2}) ([+-][0-9]{1,4}) [.] ([0-9]{1,4}) /x my ($xlen, $ylen) = map length, $x, $y; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
On Tue, May 19, 2009 at 09:55, Alexander Koenig alexander.koe...@mpi.nl wrote: snip ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; snip As of Perl 5.8 \d no longer matches [0-9]. It now matches any UNICODE character that has the digit property. This includes characters such as \x{1815} (MONGOLIAN DIGIT FIVE). You must use [0-9] if you mean [0-9] or use the bytes pragma[1] to return the old meaning of \d (but this breaks all UNICODE processing in the scope you declare it). 1. http://perldoc.perl.org/bytes.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: Simple regex question
A crude one ($part,$unit,$x,$y,$xlen,$ylen) = ($1,$2,$3,length($4),length($5)) if ($string =~ /(^\S{5})\.(\d{2})([+-])(\d+)\.(\d+)$/); -Original Message- From: Dan Fish [mailto:d...@ninemoons.com] Sent: 19 May 2009 14:18 To: beginners@perl.org Subject: Simple regex question Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits Some examples: A123C.11+002.001 FC32G.2-1.0 12B15.01+2145.15 I need all 4 pieces and the length of x y. The following works: my $id= A123C.11+002.001; my @tmp = split(/[\.+-]/,$id); my $part = $tmp[0]; my $unit = $tmp[1]; my $x = $tmp[2]; my $y = $tmp[3]; my $xlen = length $tmp[2]; my $ylen = length $tmp[3]; but in my quest for understanding regex, I'm always looking for more elegant (obfuscated :-) code... Anybody have a good one-liner for this? my ($part,$unit,$x,$y,$xlen,$ylen) = /* Some obfuscated code here.. */ Thanks! -Dan --- d...@mapson.ninemoons.com (To reply via email, remove 'reverse(nospam)' in address. Spambots are getting wy too smart nowadays...:-) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ This email is from the Press Association. For more information, see www.pressassociation.com. This email may contain confidential information. Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments. If you have received it in error, please contact the sender immediately. Any opinion expressed in this email is personal to the sender and may not reflect the opinion of the Press Association. Any email reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: Simple regex question
Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars n can be 1 or 2 digits x can be +/- (with sign), 1-4 digits y is always positive (no sign), 1-4 digits snip What do you mean by chars? Is any character valid or are only printable characters valid, or only printable ASCII characters, or a-z, A-Z, and 0-9? (snip) Agreed, should have been clearer on this... a is alphanum, a-z,A-Z,0-9 Thanks Chas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
Chas. Owens wrote on 05/19/2009 04:02 PM: ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; snip As of Perl 5.8 \d no longer matches [0-9]. It now matches any UNICODE character that has the digit property. This includes characters such as \x{1815} (MONGOLIAN DIGIT FIVE). You must use [0-9] if you mean [0-9] or use the bytes pragma[1] to return the old meaning of \d (but this breaks all UNICODE processing in the scope you declare it). Oh, I didn't know that. Thanks for pointing that out. But in most scenarios \d will still work, right? I mean, how often do you actually encounter the Mongolian Digit Five in real life data? bye Alex -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
On Tue, May 19, 2009 at 10:21, Alexander Koenig alexander.koe...@mpi.nl wrote: Chas. Owens wrote on 05/19/2009 04:02 PM: ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; snip As of Perl 5.8 \d no longer matches [0-9]. It now matches any UNICODE character that has the digit property. This includes characters such as \x{1815} (MONGOLIAN DIGIT FIVE). You must use [0-9] if you mean [0-9] or use the bytes pragma[1] to return the old meaning of \d (but this breaks all UNICODE processing in the scope you declare it). Oh, I didn't know that. Thanks for pointing that out. But in most scenarios \d will still work, right? I mean, how often do you actually encounter the Mongolian Digit Five in real life data? It isn't just \x{1815}, it is any UNICODE character with the digit property. That includes things like \x{FF15} (FULLWIDTH DIGIT FIVE) which look just like a normal number 5, but you can't do math with it. If you mean [0-9] you should say [0-9], if you mean something that looks vaguely like a number to somebody you should say \d. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
Dan Fish wrote: Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars [a-zA-Z0-9]{5} n can be 1 or 2 digits [0-9]{1,2} x can be +/- (with sign), 1-4 digits [-+][0-9]{1,4} y is always positive (no sign), 1-4 digits [0-9]{1,4} Some examples: A123C.11+002.001 FC32G.2-1.0 12B15.01+2145.15 I need all 4 pieces and the length of x y. The following works: my $id= A123C.11+002.001; my @tmp = split(/[\.+-]/,$id); my $part = $tmp[0]; my $unit = $tmp[1]; my $x = $tmp[2]; my $y = $tmp[3]; my $xlen = length $tmp[2]; my $ylen = length $tmp[3]; but in my quest for understanding regex, I'm always looking for more elegant (obfuscated :-) code... Anybody have a good one-liner for this? my ($part,$unit,$x,$y,$xlen,$ylen) = /* Some obfuscated code here.. */ my ( $part, $unit, $x, $y, $xlen, $ylen ) = ( $id =~ m{ \A ( [a-zA-Z0-9]{5} ) \. ( [0-9]{1,2} ) [-+] ( [0-9]{1,4} ) \. ( [0-9]{1,4} ) \z }x, length $3, length $4, ); John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
Chas. Owens wrote: On Tue, May 19, 2009 at 09:55, Alexander Koenig alexander.koe...@mpi.nl wrote: snip ($a,$n,$x,$y)) = $item =~ /(.{5})\.(\d\d?)[-+](\d{1,4})\.(\d{1,4})/; snip As of Perl 5.8 \d no longer matches [0-9]. ^ As of Perl 5.8 \d no longer matches only [0-9]. It now matches any UNICODE character that has the digit property. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Simple regex question
Dan Fish wrote: Simple question for the regEXperts out there... I have a string that is always in the format: a.nn+x.y a is always 5 chars [a-zA-Z0-9]{5} n can be 1 or 2 digits [0-9]{1,2} x can be +/- (with sign), 1-4 digits [-+][0-9]{1,4} y is always positive (no sign), 1-4 digits [0-9]{1,4} Some examples: A123C.11+002.001 FC32G.2-1.0 12B15.01+2145.15 I need all 4 pieces and the length of x y. The following works: my $id= A123C.11+002.001; my @tmp = split(/[\.+-]/,$id); my $part = $tmp[0]; my $unit = $tmp[1]; my $x = $tmp[2]; my $y = $tmp[3]; my $xlen = length $tmp[2]; my $ylen = length $tmp[3]; but in my quest for understanding regex, I'm always looking for more elegant (obfuscated :-) code... Anybody have a good one-liner for this? my ($part,$unit,$x,$y,$xlen,$ylen) = /* Some obfuscated code here.. */ my ( $part, $unit, $x, $y, $xlen, $ylen ) = ( $id =~ m{ \A ( [a-zA-Z0-9]{5} ) \. ( [0-9]{1,2} ) [-+] ( [0-9]{1,4} ) \. ( [0-9]{1,4} ) \z }x, length $3, length $4, ); John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
quick regex question
I need a quick regex to strip out the following: example: change remove-all-this (Keep This) into just Keep This something like: s/ beginning of line up to and including the first ( //g s/ starting from and including first ) to end of line //g Can anyone help with this quick line or 2? Thanks! Rick Bragg -- This message has been scanned for viruses and dangerous content by Green Mountain Network, and is believed to be clean. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
On Wed, Mar 25, 2009 at 12:00 PM, Rick Bragg li...@gmnet.net wrote: I need a quick regex to strip out the following: example: change remove-all-this (Keep This) into just Keep This $s =~ s/.*\((.*)\)/$1/; something like: s/ beginning of line up to and including the first ( //g s/ starting from and including first ) to end of line //g Can anyone help with this quick line or 2? Thanks! Rick Bragg -- This message has been scanned for viruses and dangerous content by Green Mountain Network, and is believed to be clean. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- [ Rodrick R. Brown ] http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
On Wed Mar 25 2009 @ 12:19, Rodrick Brown wrote: On Wed, Mar 25, 2009 at 12:00 PM, Rick Bragg li...@gmnet.net wrote: I need a quick regex to strip out the following: example: change remove-all-this (Keep This) into just Keep This $s =~ s/.*\((.*)\)/$1/; something like: s/ beginning of line up to and including the first ( //g s/ starting from and including first ) to end of line //g But if there's anything after the 'Keep this', you have problems with this version: use strict; use warnings; my $string = 'remove-all-this (Keep this) remove this too'; $string =~ s/.*\(//; $string =~ s/\).*//; print $string\n; my $string2 = 'remove-all-this (Keep this) remove this too'; $string2 =~ s/.*\((.*)\)/$1/; print $1\n if $1; print $string2\n; For $string2, you end up with Keep this remove this too since you replace the 'Keep this' back into the rest of the string. (That is, you substitute 'remove-all-this (Keep this)' with 'Keep this'. So if you have anything after the closing paren, you have a problem. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Rick Bragg wrote: I need a quick regex to strip out the following: Never heard of that. What is a quick regex? -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
On Wed, Mar 25, 2009 at 13:21, Telemachus telemac...@arpinum.org wrote: snip my $string2 = 'remove-all-this (Keep this) remove this too'; $string2 =~ s/.*\((.*)\)/$1/; snip If $string2 may contain more than one pair of parentheses, you will want to say $string2 =~ s/.*\((.*?)\)/$1/; or $string2 =~ s/.*\(([^)]*)\)/$1/; The first uses a non-greedy match (i.e. it matches the smallest string that allows the match to succeed rather than the default match of largest string that allows the match to succeed). The second matches all characters other than ) up to the next ). If you have nested parentheses (e.g. foo ( bar ( baz ) ) quux)), all bets are off, and you are better off writing a parser than trying to use a regex. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
On Wed Mar 25 2009 @ 3:10, Chas. Owens wrote: On Wed, Mar 25, 2009 at 13:21, Telemachus telemac...@arpinum.org wrote: snip my $string2 = 'remove-all-this (Keep this) remove this too'; $string2 =~ s/.*\((.*)\)/$1/; snip If $string2 may contain more than one pair of parentheses, you will want to say $string2 =~ s/.*\((.*?)\)/$1/; or $string2 =~ s/.*\(([^)]*)\)/$1/; Either I misunderstood the OP's request, or these two are still no good. They don't trim away what follows the closing parenthesis. As I understood the OP, he wanted to remove everything except the info inside of the parens. That is, what he wants left is only 'Keep this'. I think that this would work fine: my $string = 'remove-all-this (Keep this) remove this too'; $string =~ s/.*\((.*?)\).*/$1/; But as you say, if there are multiple instances or nested parens, then this is all the wrong way to go. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
quick regex question
Hi, I have two lines (well, 1 line is headers, then there follows a range of data)... # INTERFACE RADIO-NAME MAC-ADDRESS AP SIGNAL-STRENGTH TX-RATE UPTIME 0 interface_name radio 00:0C:42:1F:2C:8D yes -63...@18mbps 9Mbps 2h2m38s I'm looking for a foreach my $Line (@Output) { my ($interface, $radio, $mac, $ap, $signal, $txrate, uptime) = split(/whatidontknow/, $Line, 7); } Can anyone perhaps help out with the what I don't know bit?? FYI - The columns should be fixed lengths, if that helps perhaps... Thanks, Chris. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Chris Knipe ha scritto: Hi, I have two lines (well, 1 line is headers, then there follows a range of data)... # INTERFACE RADIO-NAME MAC-ADDRESS AP SIGNAL-STRENGTH TX-RATE UPTIME 0 interface_name radio 00:0C:42:1F:2C:8D yes -63...@18mbps 9Mbps 2h2m38s I'm looking for a foreach my $Line (@Output) { my ($interface, $radio, $mac, $ap, $signal, $txrate, uptime) = split(/whatidontknow/, $Line, 7); } Can anyone perhaps help out with the what I don't know bit?? FYI - The columns should be fixed lengths, if that helps perhaps... Thanks, Chris. Hi. Could maybe a simple split(/\s+/ $Line, 7); work? cheers paolino -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: quick regex question
# INTERFACE RADIO-NAME MAC-ADDRESS AP SIGNAL-STRENGTH TX-RATE UPTIME 0 interface_name radio 00:0C:42:1F:2C:8D yes -63...@18mbps 9Mbps 2h2m38s I'm looking for a foreach my $Line (@Output) { my ($interface, $radio, $mac, $ap, $signal, $txrate, uptime) = split(/whatidontknow/, $Line, 7); } Can anyone perhaps help out with the what I don't know bit?? FYI - The columns should be fixed lengths, if that helps perhaps... Hi. Could maybe a simple split(/\s+/ $Line, 7); work? Almost, but we're not *quite* there yet... It will do though, I think (Really just need to get $interface name and $signal)... foreach my $Line (@Output) { my ($tmp, $number, $interface, $radio, $mac, $ap, $signal, $txrate, $uptime) = split(/\s+/, $Line, 9); print Number: . $number . \n; print Interface: . $interface . \n; print Radio: . $radio . \n; print MAC:. $mac . \n; print AP: . $ap . \n; print Signal: . $signal . \n; print TX Rate:. $txrate . \n; print Uptime: . $uptime . \n; } Number:0 Interface: interface_name Radio: radio MAC: 00:0C:42:1F:2C:8D AP:yes Signal:-63dBm... TX Rate: 24Mbps Uptime: I'm not sure why I am required to have 9 fields in the split to get the values now... -- Chris. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Chris Knipe wrote: Hi, Hello, I have two lines (well, 1 line is headers, then there follows a range of data)... # INTERFACE RADIO-NAME MAC-ADDRESS AP SIGNAL-STRENGTH TX-RATE UPTIME 0 interface_name radio 00:0C:42:1F:2C:8D yes -63...@18mbps 9Mbps 2h2m38s I'm looking for a foreach my $Line (@Output) { my ($interface, $radio, $mac, $ap, $signal, $txrate, uptime) = split(/whatidontknow/, $Line, 7); } Can anyone perhaps help out with the what I don't know bit?? FYI - The columns should be fixed lengths, if that helps perhaps... If you are dealing with fixed length fields then you probably want to use unpack: perldoc -f unpack perldoc -f pack perldoc perlpacktut John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Chris Knipe ha scritto: # INTERFACE RADIO-NAME MAC-ADDRESS AP SIGNAL-STRENGTH TX-RATE UPTIME 0 interface_name radio 00:0C:42:1F:2C:8D yes -63...@18mbps 9Mbps 2h2m38s I'm looking for a foreach my $Line (@Output) { my ($interface, $radio, $mac, $ap, $signal, $txrate, uptime) = split(/whatidontknow/, $Line, 7); } Can anyone perhaps help out with the what I don't know bit?? FYI - The columns should be fixed lengths, if that helps perhaps... Hi. Could maybe a simple split(/\s+/ $Line, 7); work? Almost, but we're not *quite* there yet... It will do though, I think (Really just need to get $interface name and $signal)... foreach my $Line (@Output) { my ($tmp, $number, $interface, $radio, $mac, $ap, $signal, $txrate, $uptime) = split(/\s+/, $Line, 9); print Number: . $number . \n; print Interface: . $interface . \n; print Radio: . $radio . \n; print MAC:. $mac . \n; print AP: . $ap . \n; print Signal: . $signal . \n; print TX Rate:. $txrate . \n; print Uptime: . $uptime . \n; } Number:0 Interface: interface_name Radio: radio MAC: 00:0C:42:1F:2C:8D AP:yes Signal:-63dBm... TX Rate: 24Mbps Uptime: I'm not sure why I am required to have 9 fields in the split to get the values now... -- Chris. I am not sure about what's not working for you... It seems to work fine for me: my ($number, $interface, $radio, $mac, $ap, $signal, $txrate, $uptime) = split(/\s+/, $Line, 8); print Number: . $number . \n; print Interface: . $interface . \n; print Radio: . $radio . \n; print MAC:. $mac . \n; print AP: . $ap . \n; print Signal: . $signal . \n; print TX Rate:. $txrate . \n; print Uptime: . $uptime . \n; and 8 should be right, 'cause it's 8 fields you have there.. Could you clarify what you find not working? Cheers paolino -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Chris Knipe wrote: Paolo Gianrossi wrote: Could maybe a simple split(/\s+/ $Line, 7); work? Almost, but we're not *quite* there yet... ... I'm not sure why I am required to have 9 fields in the split to get the values now... It appears from your initial post as if there is a leading space before the zero value. If that's the case, your question is answered at the very first line of perldoc -f split. If you exchange /\s+/ for ' ' you can skip that $tmp variable. No need to use the LIMIT parameter, btw. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick regex question
Chris Knipe wrote: (Really just need to get $interface name and $signal)... Then you may prefer a list slice. my ($interface, $signal) = (split ' ', $Line)[1,5]; -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Regex question.
Hi all, Thanks for all the help provided so far in my earlier queries. I have a query regarding regexes. I am to match only one specific word in a wordlist which has only those alphabets which it contains and none other than that. For example: My Wordlist contains: alpha alpha1 beta betaze gamma gamman gammano I want to match any scrambled word with a word in the wordlist which has exactly the same alphabets may be unscrambled/scrambled no other alphabets. Say, If I enter 'ammag' (scrambled word for gamma), it should only match 'gamma' and NOT 'gamman' | 'gammano'(which my current code is doing). Here is the code: - use strict; use warnings; my($a,$b,$c,$d,$e,$f); while(1) { print Enter a word: ; my $this=STDIN; chomp($this); ($a,$b,$c,$d,$e,$f)=split('',$this); open (FH,'wordlist.txt'); while (FH) { if (($_=~ /$a/) ($_=~ /$b/) ($_=~ /$c/) ($_=~ /$d/) ($_=~ /$e/) ($_=~ /$f/)) {print $_;} } close FH; } --- The wordlist contains even 5/6/7 alphabet words.My second thought on this is that may be I also need to determine length of the words and accordingly, build up the search criteria, so to match exactly the same length of word in the wordlist. Any suggestions? Thanks -- Gunwant Singh. What is the sound of Perl? Is it not the sound of a wall that people bang their heads against? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
anagrams (was: Re: Regex question.)
Gunwant Singh schreef: I have a query regarding regexes. No, you don't. I am to match only one specific word in a wordlist which has only those alphabets which it contains and none other than that. To do that, you can use regexes, but you don't need to. My Wordlist contains: alpha alpha1 beta betaze gamma gamman gammano I want to match any scrambled word with a word in the wordlist which has exactly the same alphabets may be unscrambled/scrambled no other alphabets. Say, If I enter 'ammag' (scrambled word for gamma), it should only match 'gamma' and NOT 'gamman' | 'gammano' Then the first thing you will check is the length. You don't need a regex to do that, though you can use a regex, but normally you would use the builtin function length(). (see `perldoc -f length`) After this you need to compare the character frequencies. An easy way to do that is to transform your word to a scrambled version where the characters are sorted internally (gamma - aagmm). You can do that once with your Wordlist (and store that as Wordlist.unified). perl -wle ' $word = Gamma; $sorted = join , sort split , lc($word); print $sorted; ' aagmm -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question matching dates
John W. Krahn wrote: } elsif ( $ARGV[0] =~ m/\b2008(0[1-9]|1[12])(0[1-9]|1[0-9]|2[0-9]|3[01])([01][0-9]|2[0-3])\b/ ) { ^ So you don't want to test for October? John fixed now. thanks!! } elsif ( $ARGV[0] =~ m/\b2008(0[1-9]|1[012])(0[1-9]|1[0-9]|2[0-9]|3[01])([01][0-9]|2[0-3])\b/ ) { @array = qx#ls -tr $directory/$ARGV[0]*#; $hour = substr($ARGV[0], 8 , 2); $date_1 = substr($ARGV[0], 4 , 4); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
regex question matching dates
given then ARGV[0] is 2008052803, why woulnd't below regex match them?? } elsif ( $ARGV[0] =~ m/\b2008[01][1-31]([01][0-9]|2[0-3])\b/ ) { @array = qx#ls -tr $directory/$ARGV[0]*#; #2008052803 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question matching dates
Richard Lee wrote: given then ARGV[0] is 2008052803, why woulnd't below regex match them?? 2008052803 is a ten digit number. } elsif ( $ARGV[0] =~ m/\b2008[01][1-31]([01][0-9]|2[0-3])\b/ ) { Your pattern matches eight digits with a \b word boundary at each end so it will never match a ten digit number. Also the character class [1-31] could be more simply written as [1-3] (repeated characters are ignored.) @array = qx#ls -tr $directory/$ARGV[0]*#; Why not do that directly in perl: @array = map $_-[0], sort { $a-[1] = $b-[1] } map [ $_, -M ], glob $directory/$ARGV[0]*; #2008052803 John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question matching dates
John W. Krahn wrote: Richard Lee wrote: @array = qx#ls -tr $directory/$ARGV[0]*#; Why not do that directly in perl: @array = map $_-[0], sort { $a-[1] = $b-[1] } map [ $_, -M ], glob $directory/$ARGV[0]*; Sorry, that should be: @array = map $_-[0], sort { $b-[1] = $a-[1] } map [ $_, -M ], glob $directory/$ARGV[0]*; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question matching dates
John W. Krahn wrote: Richard Lee wrote: given then ARGV[0] is 2008052803, why woulnd't below regex match them?? 2008052803 is a ten digit number. } elsif ( $ARGV[0] =~ m/\b2008[01][1-31]([01][0-9]|2[0-3])\b/ ) { Your pattern matches eight digits with a \b word boundary at each end so it will never match a ten digit number. Also the character class [1-31] could be more simply written as [1-3] (repeated characters are ignored.) @array = qx#ls -tr $directory/$ARGV[0]*#; Why not do that directly in perl: @array = map $_-[0], sort { $a-[1] = $b-[1] } map [ $_, -M ], glob $directory/$ARGV[0]*; #2008052803 John hey thanks! this works fine now } elsif ( $ARGV[0] =~ m/\b2008(0[1-9]|1[12])(0[1-9]|1[0-9]|2[0-9]|3[01])([01][0-9]|2[0-3])\b/ ) { @array = qx#ls -tr $directory/$ARGV[0]*#; #2008052803 } else { I will try that direct perl solution as well!! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question matching dates
Richard Lee wrote: John W. Krahn wrote: Richard Lee wrote: given then ARGV[0] is 2008052803, why woulnd't below regex match them?? 2008052803 is a ten digit number. } elsif ( $ARGV[0] =~ m/\b2008[01][1-31]([01][0-9]|2[0-3])\b/ ) { Your pattern matches eight digits with a \b word boundary at each end so it will never match a ten digit number. Also the character class [1-31] could be more simply written as [1-3] (repeated characters are ignored.) @array = qx#ls -tr $directory/$ARGV[0]*#; Why not do that directly in perl: @array = map $_-[0], sort { $a-[1] = $b-[1] } map [ $_, -M ], glob $directory/$ARGV[0]*; hey thanks! this works fine now } elsif ( $ARGV[0] =~ m/\b2008(0[1-9]|1[12])(0[1-9]|1[0-9]|2[0-9]|3[01])([01][0-9]|2[0-3])\b/ ) { ^ So you don't want to test for October? John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
On Fri, Apr 25, 2008 at 12:47 AM, David Nicholas Kayal [EMAIL PROTECTED] wrote: why is the first one true? check 'perldoc perlre' : The following standard quantifiers are recognized: * Match 0 or more times \d* means 0 or more numbers. -- J. Peng - QQMail Operation Team eMail: [EMAIL PROTECTED] AIM: JeffHua -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
regex question
[EMAIL PROTECTED] quick_test]$ cat test_regex.pl #!/usr/bin/perl -w use strict; my $line = test_text; if($line =~ m/^\d*/) { print true\n; } else { print false\n; } if($line =~ m/^\d/) { print true\n; } else { print false\n; } [EMAIL PROTECTED] quick_test]$ ./test_regex.pl true false [EMAIL PROTECTED] quick_test]$ why is the first one true? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
Thank you. On Fri, 25 Apr 2008, J. Peng wrote: On Fri, Apr 25, 2008 at 12:47 AM, David Nicholas Kayal [EMAIL PROTECTED] wrote: why is the first one true? check 'perldoc perlre' : The following standard quantifiers are recognized: * Match 0 or more times \d* means 0 or more numbers. -- J. Peng - QQMail Operation Team eMail: [EMAIL PROTECTED] AIM: JeffHua -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: basic regex question
The primary problem would be that you're match seems a bit backwards. Try if ($line =~ m/$file/) which will cut through any extra formatting output by dir. Jim wrote: Hi Everyone, I am trying to match file subdir names from a back tic generated list. I never find anything and there are files in the directory with a correct name. but if i make up a list to use as filenames it works as i expect ... any insight greatly appreciated thanks Jim #!/usr/bin/perl use strict; use warnings; my @files = qw (filename1 filename2 filename3 filename4); #my @dir = qw (file filenaml filename filename2); my @dir = `dir`; foreach my $file (@files) { foreach my $line (@dir) { if ($file =~ m/$line/) { print \nfound one\n; } print file - $file ; print line - $line\n; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
basic regex question
Hi Everyone, I am trying to match file subdir names from a back tic generated list. I never find anything and there are files in the directory with a correct name. but if i make up a list to use as filenames it works as i expect ... any insight greatly appreciated thanks Jim #!/usr/bin/perl use strict; use warnings; my @files = qw (filename1 filename2 filename3 filename4); #my @dir = qw (file filenaml filename filename2); my @dir = `dir`; foreach my $file (@files) { foreach my $line (@dir) { if ($file =~ m/$line/) { print \nfound one\n; } print file - $file ; print line - $line\n; } } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
YARQ: Yet Another Regex Question
I have a trouble ticket application that uses a regex to find a piece of information in an incoming email and auto populate a field if it is found. The line it will be looking for is CUSTOMER ENVIRONMENT customer_name where customer_name will never have a space making it one word. If I just want to pull from the line the customer_name would my regex look like $MatchString = CUSTOMER ENVIRONMENT\s+(\w) For what it's worth the line that will handle this is $found = ($Transaction-Attachments-First-Content =~ /$MatchString/m); I'm guessing that when used in an assignment like this, $1 will be used as the value. The contents of (\w) in this case. Is that correct? Mathew -- Keep up with me and what I'm up to: http://theillien.blogspot.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: YARQ: Yet Another Regex Question
On 5/16/07, Mathew Snyder [EMAIL PROTECTED] wrote: I have a trouble ticket application that uses a regex to find a piece of information in an incoming email and auto populate a field if it is found. The line it will be looking for is CUSTOMER ENVIRONMENT customer_name where customer_name will never have a space making it one word. If I just want to pull from the line the customer_name would my regex look like $MatchString = CUSTOMER ENVIRONMENT\s+(\w) Bad idea. Use qr() instead. For what it's worth the line that will handle this is $found = ($Transaction-Attachments-First-Content =~ /$MatchString/m); I'm guessing that when used in an assignment like this, $1 will be used as the value. The contents of (\w) in this case. Is that correct? snip Yes, the $1 match variable will hold the match if $found is true. A common idiom is therefore my $name; my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/; if ($Transaction-Attachments-First-Content =~ /$regex) { $name = $1; } else { die could not find name; } Another way to write this is my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/; my ($name) = $Transaction-Attachments-First-Content =~ /$regex/ or die could not find name; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: YARQ: Yet Another Regex Question
Chas Owens wrote: On 5/16/07, Mathew Snyder [EMAIL PROTECTED] wrote: I have a trouble ticket application that uses a regex to find a piece of information in an incoming email and auto populate a field if it is found. The line it will be looking for is CUSTOMER ENVIRONMENT customer_name where customer_name will never have a space making it one word. If I just want to pull from the line the customer_name would my regex look like $MatchString = CUSTOMER ENVIRONMENT\s+(\w) Bad idea. Use qr() instead. For what it's worth the line that will handle this is $found = ($Transaction-Attachments-First-Content =~ /$MatchString/m); I'm guessing that when used in an assignment like this, $1 will be used as the value. The contents of (\w) in this case. Is that correct? snip Yes, the $1 match variable will hold the match if $found is true. A common idiom is therefore my $name; my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/; if ($Transaction-Attachments-First-Content =~ /$regex) { $name = $1; } else { die could not find name; } Another way to write this is my $regex = qr/CUSTOMER ENVIRONMENT\s+(\w)/; my ($name) = $Transaction-Attachments-First-Content =~ /$regex/ or die could not find name; What does gr() do? Mathew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: YARQ: Yet Another Regex Question
On 5/16/07, Mathew [EMAIL PROTECTED] wrote: snip What does gr() do? Mathew qr not gr. It is the quote regex operator. from perldoc perlop qr/STRING/imosx This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in m/PATTERN/. If ' is used as the delimiter, no interpolation is done. Returns a Perl value which may be used instead of the corresponding /STRING/imosx expression. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: YARQ: Yet Another Regex Question
Mathew 写道: What does gr() do? It's qr not gr. See perldoc perlop and look for qr/STRING/imosx. -- http://home.arcor.de/jeffpang/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: YARQ: Yet Another Regex Question
Chas Owens wrote: On 5/16/07, Mathew [EMAIL PROTECTED] wrote: snip What does gr() do? Mathew qr not gr. It is the quote regex operator. from perldoc perlop qr/STRING/imosx This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in m/PATTERN/. If ' is used as the delimiter, no interpolation is done. Returns a Perl value which may be used instead of the corresponding /STRING/imosx expression. Ahh, yes that would certainly work better. One of my concerns was using a bare string containing a space. While it may not break it could cause problems down the road. Something I'd like to avoid. Thanks Mathew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
John W. Krahn writes: Tom Allison wrote: How do I pull all the words from a line between the two words 'from' and 'by' when I have NO IDEA what's in there, but I know they are all in one line. To make it more difficult. 'by' is optional... Like this: from..by.. or from.. I want all the stuff inside. Initially I'm thinking /from (.+?) (?:by (.+?))?/ Anything better? $ perl -le' for ( abc from to the word by and the end, abc from to the end ) { print $1 if /from(.*?(?=by)|.*)/; } ' to the word to the end I have no Perl on this PC to test this, but I would have thought print $1 if /\bfrom\s+(.*?)\s*(?:\bby\b|$)/; would do the trick. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
regex question
How do I pull all the words from a line between the two words 'from' and 'by' when I have NO IDEA what's in there, but I know they are all in one line. To make it more difficult. 'by' is optional... Like this: from..by.. or from.. I want all the stuff inside. Initially I'm thinking /from (.+?) (?:by (.+?))?/ Anything better? I can negate a character with [^b] to mean not-'b' but can I negate a word? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
# Requires by: $line = daffromHello Worldby; $line =~ /from(.*)(by)/; print $1; Not sure about making it optional. Can always check if you got and then try without the by On 5/14/07, Tom Allison [EMAIL PROTECTED] wrote: How do I pull all the words from a line between the two words 'from' and 'by' when I have NO IDEA what's in there, but I know they are all in one line. To make it more difficult. 'by' is optional... Like this: from..by.. or from.. I want all the stuff inside. Initially I'm thinking /from (.+?) (?:by (.+?))?/ Anything better? I can negate a character with [^b] to mean not-'b' but can I negate a word? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
yitzle wrote: # Requires by: $line = daffromHello Worldby; $line =~ /from(.*)(by)/; print $1; Not sure about making it optional. Can always check if you got and then try without the by the .* pulls in too much. I'm going to have to back up and try this again. But I think it's late. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
Tom Allison wrote: How do I pull all the words from a line between the two words 'from' and 'by' when I have NO IDEA what's in there, but I know they are all in one line. To make it more difficult. 'by' is optional... Like this: from..by.. or from.. I want all the stuff inside. Initially I'm thinking /from (.+?) (?:by (.+?))?/ Anything better? $ perl -le' for ( abc from to the word by and the end, abc from to the end ) { print $1 if /from(.*?(?=by)|.*)/; } ' to the word to the end John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
John W. Krahn 写道: $ perl -le' for ( abc from to the word by and the end, abc from to the end ) { print $1 if /from(.*?(?=by)|.*)/; } ' But this coundn't handle this case: $string = abc from to the word byebye...; Maybe a space following the by is needed? :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regex question
Hi, On May 14, 2007, at 11:50 PM, Tom Allison wrote: yitzle wrote: # Requires by: $line = daffromHello Worldby; $line =~ /from(.*)(by)/; print $1; Not sure about making it optional. Can always check if you got and then try without the by the .* pulls in too much. I'm going to have to back up and try this again. But I think it's late. You can use '?' for the not greedy way $line =~ m/ from \s+ (.*?) # anything, non greedy (?: \s+ by \s+ (.*) )? $/smx; Good luck! -- Igor Sutton [EMAIL PROTECTED] PGP.sig Description: This is a digitally signed message part
Re: regex question
On 5/14/07, Jeff Pang [EMAIL PROTECTED] wrote: snip Maybe a space following the by is needed? :) snip This what the word boundary character class is for perl -le' for ( abc from to the word by and the end, abc from to the end, abc from this byebye to this) { print $1 if /from(.*?(?=\bby\b)|.*)/; } ' -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: split w. regex question/problem
John W. Krahn wrote: Rob Dixon wrote: John W. Krahn wrote: Gauthier, Dave wrote: Getting unwanted list elements when using split with regex. Here's an example $str = abc=In; @arr = split(/[a-zA-Z0-9]/,$str); foreach $a (@arr) {print $a\n;} I get... = ^ ^ ^ ^ O T T F n w h o e o r u e r e If I change abc=In to abcdef=In, I get 6 unwanetd null elements (one per char before the =). I was expectiing a single element list with arr[0] = =. What's Up ?Is ther a clen way to prevent creating these unwanted elements? In your example the string abc=In is being split using the expression /[a-zA-Z0-9]/. That expression matches in the string at positions 0, 1, 2, 4 and 5 therefore split will produce a list of *six* elements: $ perl -le' $str = abc=In; print ++$x, : $_ for split /[a-zA-Z0-9]/, $str, -1; ' 1: 2: 3: 4: = 5: 6: Your example only shows four because split automatically discards any trailing empty elements. [snip] Er, but he shows /six/ elements; and split /doesn't/ discard trailing empty elements. Or am I misunderstanding you John? perldoc -f split split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.) Thanks John. I was reading the wrong part of the post, and you showed me a corner of Perl that I didn't know about. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: split w. regex question/problem
John W. Krahn schreef: split Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, For which meanings of 'default'? :) perl -wle ' $_ = a b c ; @x = split; print scalar @x; ' 3 -- Affijn, Ruud Gewoon is een tijger. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: split w. regex question/problem
John W. Krahn wrote: Gauthier, Dave wrote: Getting unwanted list elements when using split with regex. Here's an example $str = abc=In; @arr = split(/[a-zA-Z0-9]/,$str); foreach $a (@arr) {print $a\n;} I get... = If I change abc=In to abcdef=In, I get 6 unwanetd null elements (one per char before the =). I was expectiing a single element list with arr[0] = =. What's Up ?Is ther a clen way to prevent creating these unwanted elements? In your example the string abc=In is being split using the expression /[a-zA-Z0-9]/. That expression matches in the string at positions 0, 1, 2, 4 and 5 therefore split will produce a list of *six* elements: $ perl -le' $str = abc=In; print ++$x, : $_ for split /[a-zA-Z0-9]/, $str, -1; ' 1: 2: 3: 4: = 5: 6: Your example only shows four because split automatically discards any trailing empty elements. [snip] Er, but he shows /six/ elements; and split /doesn't/ discard trailing empty elements. Or am I misunderstanding you John? Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: split w. regex question/problem
Rob Dixon wrote: John W. Krahn wrote: Gauthier, Dave wrote: Getting unwanted list elements when using split with regex. Here's an example $str = abc=In; @arr = split(/[a-zA-Z0-9]/,$str); foreach $a (@arr) {print $a\n;} I get... = ^ ^ ^ ^ O T T F n w h o e o r u e r e If I change abc=In to abcdef=In, I get 6 unwanetd null elements (one per char before the =). I was expectiing a single element list with arr[0] = =. What's Up ?Is ther a clen way to prevent creating these unwanted elements? In your example the string abc=In is being split using the expression /[a-zA-Z0-9]/. That expression matches in the string at positions 0, 1, 2, 4 and 5 therefore split will produce a list of *six* elements: $ perl -le' $str = abc=In; print ++$x, : $_ for split /[a-zA-Z0-9]/, $str, -1; ' 1: 2: 3: 4: = 5: 6: Your example only shows four because split automatically discards any trailing empty elements. [snip] Er, but he shows /six/ elements; and split /doesn't/ discard trailing empty elements. Or am I misunderstanding you John? perldoc -f split split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.) John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
split w. regex question/problem
Getting unwanted list elements when using split with regex. Here's an example $str = abc=In; @arr = split(/[a-zA-Z0-9]/,$str); foreach $a (@arr) {print $a\n;} I get... = If I change abc=In to abcdef=In, I get 6 unwanetd null elements (one per char before the =). I was expectiing a single element list with arr[0] = =. What's Up ?Is ther a clen way to prevent creating these unwanted elements? Thanks in Advance -dave
Re: split w. regex question/problem
On 2/2/07, Gauthier, Dave [EMAIL PROTECTED] wrote: Getting unwanted list elements when using split with regex. Here's an example @arr = split(/[a-zA-Z0-9]/,$str); Your separator is a single character. Did you want to split on runs of one or more single characters? /[a-zA-Z0-9]+/ And is there a reason you're not using the obvious shortcut? /\w+/ Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/