Re: Regular Expressions Question
On Apr 10, 11:03 pm, jwkr...@shaw.ca (John W. Krahn) wrote: cityuk wrote: Dear All, Hello, This is more of a generic question on regular expressions as my program is working fine but I was just curious. Say you have the following URLs: http://www.test.com/image.gif http://www.test.com/?src=image.gif?width=12 I want to get the type of the image, i.e. the string gif. For the first URL the regular expression .*\.([a-z]{3}) will do the trick while for the second one I am using .*=\([a-z]{3})?.*. Ignoring the fact that the REs can be written better my question is: If I put them together, that is write them as .*\.([a-z]{3})|.*=\([a-z]{3})?.* perl thinks that the or only applies to the characters immediately surrounding it (in this case ) and .). No. The alternation applies to the complete pattern '.*\.([a-z]{3})' OR OK. So if I understood you correctly, given the following (actual) URLs http://beta.images.theglobeandmail.com/archive/01258/election_heads__1258993cl-3.jpg http://storage.canoe.ca/v1/dynamic_resize/?src=http://www.torontosun.com/news/decision2011/2011/04/06/300_harper_boring.jpgsize=248x186 the following pattern ^\s*.*\.([a-zA-z]{3})$ | ^\S*\?\S*\.([a-zA-z]{3}).*$ should match them both. Am I correct? Regards, George '.*=\([a-z]{3})?.*'. 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/
Re: Regular Expressions Question
On Apr 11, 7:21 am, gklc...@googlemail.com (gkl) wrote: On Apr 10, 11:03 pm, jwkr...@shaw.ca (John W. Krahn) wrote: stion on regular expressions as my program is working fine but I was just curious. Say you have the following URLs: http://www.test.com/image.gif http://www.test.com/?src=image.gif?width=12 OK. So if I understood you correctly, given the following (actual) URLs http://beta.images.theglobeandmail.com/archive/01258/election_heads__...http://storage.canoe.ca/v1/dynamic_resize/?src=http://www.torontosun the following pattern ^\s*.*\.([a-zA-z]{3})$ | ^\S*\?\S*\.([a-zA-z]{3}).*$ should match them both. Am I correct? No, there is at least one problem. In your first alternative, the '.*' will also match the literal '?' which the second alternative is matching. See: 'perldoc perlretut' for a review. [ The URI module which was mentioned will be a quicker solution and will work work all cases. ] -- Charles DeRykus See: perldoc perlretut -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions Question
On 11/04/2011 15:21, gkl wrote: OK. So if I understood you correctly, given the following (actual) URLs http://beta.images.theglobeandmail.com/archive/01258/election_heads__1258993cl-3.jpg http://storage.canoe.ca/v1/dynamic_resize/?src=http://www.torontosun.com/news/decision2011/2011/04/06/300_harper_boring.jpgsize=248x186 the following pattern ^\s*.*\.([a-zA-z]{3})$ | ^\S*\?\S*\.([a-zA-z]{3}).*$ should match them both. Am I correct? First of all I notice that the src parameter in your second URL's query is now an absolute URL, whereas your first post had just a file name. Since we cannot anticipate how far and in which direction your problem may grow, it is your responsibility to present the entirety of the possibilities as you know them. Otherwise you will be engaging the world in a goose chase of the wildest sort. If you mean /^\s*.*\.([a-zA-z]{3})$ | ^\S*\?\S*\.([a-zA-z]{3}).*$/ then you must apply the /x modifier, otherwise the spaces at the end of the first option and at the beginning of the second form part of the expressions. As far as I can think, /^\s*.*\.([a-zA-z]{3})$/ is exactly equivalent to /\.([a-zA-z]{3})$/ which, presumably as you intend, will match the first URL and capture 'jpg'. It will fail to match the second URL. While the first option seemed to be considering the possibility of irrelevant leading spaces, the second /^\S*\?\S*\.([a-zA-z]{3}).*$/ is insisting on a sequence of non-spaces from the beginning of the string up to the last possible question mark. Then another sequence of non-spaces up to the last possible dot, followed by three alphas and an ampersand. The subsequent /.*$/ does nothing. I suggest to you that simply /.*\.([a-z]+)/i will match all of the four URLs you have posted so far, and capture from them exactly what you expect. Only you can know the full extent of your problem, and why you refuse the advice you have been offered. I will continue to try to help you. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions Question
On 11/04/2011 06:43, Shlomi Fish wrote: On Sunday 10 Apr 2011 14:05:49 cityuk wrote: This is more of a generic question on regular expressions as my program is working fine but I was just curious. Say you have the following URLs: http://www.test.com/image.gif http://www.test.com/?src=image.gif?width=12 Don't use regular expressions to parse URLs - instead use URI.pm: http://cpan.uwinnipeg.ca/dist/URI I agree. The program below shows a subroutine which will extract the file type from either form of URL. It first checks to see if there is a 'src' option in the query, using this for the file name if so; otherwise it uses the last segment of the URL path. The file type type is extracted by capturing all trailing non-dot characters from the file name. (I assume your second address should read http://www.test.com/?src=image.gifwidth=12 with an ampersand instead of a second question mark?) HTH, Rob use strict; use warnings; use URI; sub filetype_from_url { my $url = URI-new($_[0]); my %form = $url-query_form; my $file = $form{src} || ($url-path_segments)[-1]; return $file =~ /([^.]+)\z/; } print filetype_from_url('http://www.test.com/image.gif'), \n; print filetype_from_url('http://www.test.com/?src=image.gifwidth=12'), \n; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions Question
cityuk wrote: Dear All, Hello, This is more of a generic question on regular expressions as my program is working fine but I was just curious. Say you have the following URLs: http://www.test.com/image.gif http://www.test.com/?src=image.gif?width=12 I want to get the type of the image, i.e. the string gif. For the first URL the regular expression .*\.([a-z]{3}) will do the trick while for the second one I am using .*=\([a-z]{3})?.*. Ignoring the fact that the REs can be written better my question is: If I put them together, that is write them as .*\.([a-z]{3})|.*=\([a-z]{3})?.* perl thinks that the or only applies to the characters immediately surrounding it (in this case ) and .). No. The alternation applies to the complete pattern '.*\.([a-z]{3})' OR '.*=\([a-z]{3})?.*'. 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/
Re: Regular Expressions Question
On 04/10/2011 04:05 AM, cityuk wrote: Is there a way to say here is a whole RE, here is another and match the first or the second? Jeffrey E.F. Friedl, 2006, Mastering Regular Expressions, 3 e., O'Reilly Media, ISBN 978-0-596-52812-6. http://oreilly.com/catalog/9780596528126/ HTH, David -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions Question
On Sunday 10 Apr 2011 14:05:49 cityuk wrote: Dear All, This is more of a generic question on regular expressions as my program is working fine but I was just curious. Say you have the following URLs: http://www.test.com/image.gif http://www.test.com/?src=image.gif?width=12 Don't use regular expressions to parse URLs - instead use URI.pm: http://cpan.uwinnipeg.ca/dist/URI Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ http://www.shlomifish.org/humour/ways_to_do_it.html Electrical Engineering studies. In the Technion. Been there. Done that. Forgot a lot. Remember too much. 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: Regular expressions question
Can anyone tell me how to write a regular expression which matches anything _except_ a litteral string ? One could also use a zero-with negative look-ahead assertion: #!/usr/bin/perl -w use strict; while( my $line = DATA ){ if( $line =~ m/^(?!Nomatch)/ ){ print match: $line; } } Thanks a lot for the reply, that worked perfectly in my application. David -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular expressions question
2009/11/17 mangled...@yahoo.com mangled...@yahoo.com: Hi, Hello, Can anyone tell me hoq to write a regular expression which matches anything _except_ a litteral string ? For instance, I want to match any line which does not begin with Nomatch. So in the following : Line1 Line2 Nomatch Line3 Line 4 I would match every line except the one containing Nomatch You would negate the pattern. Something like this: #!/usr/bin/perl use strict; use warnings; while (DATA) { print if ! /^Nomatch/; } __DATA__ Line1 Line2 Nomatch Line3 Line 4 ~ Output: Line1 Line2 Line3 Line 4 see perldoc perlop #Logical-Not and perldoc perlsyn and of course perldoc perlrequick HTH, Dp. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular expressions question
On Wed, Nov 18, 2009 at 5:05 PM, Thomas Bätzler t.baetz...@bringe.comwrote: Hi, Dermot paik...@googlemail.com suggested: 2009/11/17 mangled...@yahoo.com mangled...@yahoo.com: Can anyone tell me hoq to write a regular expression which matches anything _except_ a litteral string ? For instance, I want to match any line which does not begin with Nomatch. So in the following : You would negate the pattern. Something like this: #!/usr/bin/perl use strict; use warnings; while (DATA) { print if ! /^Nomatch/; } __DATA__ Line1 Line2 Nomatch Line3 Line 4 One could also use a zero-with negative look-ahead assertion: #!/usr/bin/perl -w use strict; while( my $line = DATA ){ if( $line =~ m/^(?!Nomatch)/ ){ print match: $line; } } __DATA__ Line1 Line2 Nomatch Line3 Line 4 Cheers, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ Look ahead notation works only on relatively recent versions of Perl, if your environment contains things like HP-UX that ships with a decades old version of Perl 5.005 I believe it is (depending on the version of HP-UX of course) you might get in trouble. I would therefore not use it or make the script explicitly require 5.6 or higher just in case. Regards, Rob
Re: Regular Expressions with Incremented Variable Embedded
Raabe, Wesley wrote: I am using regular expressions to alter a text file. Where my original file has three spaces to start a paragraph, I want to replace each instance of three spaces with a bracketed paragraph number, with a counter for paragraph numbers, pgf 1, pgf 2, pgf 3 etc. [...] The WHILE loop that I've crafted is like this: while (IN) { chomp; s/\ \ \ /\pgf\ (?{my $para_num = 1; $para_num++;){print $para_num;}})\/gi; # Replace three spaces with pgf XX print OUT $_\n; } I'm trying to embed the PERL code based on the PERL tutorial (http://perldoc.perl.org/perlretut.html#A-bit-of-magic%3a-executing-Perl-code-in-a-regular-expression, which is noted as an experimental feature. But it doesn't work (using MAC OSX). The output in my text file is pgf (?{my = 1; ++;){print ;}}) at start of each paragraph. Is there a way to do this with AUTO-INCREMENT variable and a FOR loop outside the regular expression in which the value is inserted inside the regular expression? My earlier attempts to do it that way always resulted in no change in the value, just pgf 1 on every paragraph time. I don't understand your g-modifier. Why is it there? I assume that you only want to make the substitution at the start of a line. #!/usr/bin/perl -w use strict; my $fname_inp = test.inp; my $fname_oup = test.oup; { open my $fh_inp, , $fname_inp or die '$fname_inp': , $!; open my $fh_oup, , $fname_oup or die '$fname_oup': , $!; my $pgf = 1; while ( $fh_inp ) { s/^[ ]{3}/pgf $pgf/ and $pgf++; print $fh_oup $_; } close $fh_oup or die '$fname_oup': , $!; } __END__ -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions with Incremented Variable Embedded
Raabe, Wesley wrote: I am using regular expressions to alter a text file. Where my original file has three spaces to start a paragraph, I want to replace each instance of three spaces with a bracketed paragraph number, with a counter for paragraph numbers, pgf 1, pgf 2, pgf 3 etc. The PERL program that I'm using is modeled on the answer to chapter 9, question 3 in the Learning Perl book (4th ed.). The WHILE loop that I've crafted is like this: while (IN) { chomp; s/\ \ \ /\pgf\ (?{my $para_num = 1; $para_num++;){print $para_num;}})\/gi; # Replace three spaces with pgf XX print OUT $_\n; } I'm trying to embed the PERL code based on the PERL tutorial (http://perldoc.perl.org/perlretut.html#A-bit-of-magic%3a-executing- Perl-code-in-a-regular-expression, which is noted as an experimental feature. But it doesn't work (using MAC OSX). The output in my text file is pgf (?{my = 1; ++;){print ;}}) at start of each paragraph. Is there a way to do this with AUTO-INCREMENT variable and a FOR loop outside the regular expression in which the value is inserted inside the regular expression? My earlier attempts to do it that way always resulted in no change in the value, just pgf 1 on every paragraph time. my $para_num; while ( IN ) { s/ /pgf @{[++$para_num]}/g; print OUT; } 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: Regular Expressions with Incremented Variable Embedded
On Sat, May 30, 2009 at 23:32, Raabe, Wesley wra...@kent.edu wrote: I am using regular expressions to alter a text file. Where my original file has three spaces to start a paragraph, I want to replace each instance of three spaces with a bracketed paragraph number, with a counter for paragraph numbers, pgf 1, pgf 2, pgf 3 etc. The PERL program that I'm using is modeled on the answer to chapter 9, question 3 in the Learning Perl book (4th ed.). The WHILE loop that I've crafted is like this: while (IN) { chomp; s/\ \ \ /\pgf\ (?{my $para_num = 1; $para_num++;){print $para_num;}})\/gi; # Replace three spaces with pgf XX print OUT $_\n; } I'm trying to embed the PERL code based on the PERL tutorial (http://perldoc.perl.org/perlretut.html#A-bit-of-magic%3a-executing-Perl-code-in-a-regular-expression, which is noted as an experimental feature. But it doesn't work (using MAC OSX). The output in my text file is pgf (?{my = 1; ++;){print ;}}) at start of each paragraph. Is there a way to do this with AUTO-INCREMENT variable and a FOR loop outside the regular expression in which the value is inserted inside the regular expression? My earlier attempts to do it that way always resulted in no change in the value, just pgf 1 on every paragraph time. snip That would be because the second part of a s/// is not a regex, it is a double quote string. What you want is the /e option which interprets the second part as Perl code instead: my $i = 0; while (IN) { s/[ ]{3}/pgf . $i++ . /ge; print; } -- 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: Regular Expressions
Chas. Owens wrote: On Sat, Feb 7, 2009 at 19:11, Gunnar Hjalmarsson nore...@gunnar.cc wrote: TMTOWTDI use Time::Local; while (DATA) { s{,(.+?),}{ my ($d, $m, $y) = split /\//, $1; my $t = timelocal 0, 0, 0, $d, $m-1, $y; ($d, $m, $y) = (localtime $t)[3..5]; sprintf ',%d-%02d-%02d,', $y+1900, $m+1, $d; }e; } snip And this would be the confusing, fragile mess I spoke of. Sorry, but I fail too see how using the s/// operator to extract the date field would be so much more confusing and fragile compared to split() + join(). -- 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: Regular Expressions
On Sun, Feb 8, 2009 at 03:49, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Chas. Owens wrote: On Sat, Feb 7, 2009 at 19:11, Gunnar Hjalmarsson nore...@gunnar.cc wrote: TMTOWTDI use Time::Local; while (DATA) { s{,(.+?),}{ my ($d, $m, $y) = split /\//, $1; my $t = timelocal 0, 0, 0, $d, $m-1, $y; ($d, $m, $y) = (localtime $t)[3..5]; sprintf ',%d-%02d-%02d,', $y+1900, $m+1, $d; }e; } snip And this would be the confusing, fragile mess I spoke of. Sorry, but I fail too see how using the s/// operator to extract the date field would be so much more confusing and fragile compared to split() + join(). snip You are calling three functions (one of which is split) and assigning returns three times inside the replacement. Add on top of that the fact that the regex only works for the second field. Compare all of that to calling two much simpler functions, a simple substitution, and one assignment. Try to imagine what happens six months from now when you need to go back and perform a transformation on the fifth field. Are you going to extend the regex to try to capture that value? Or are you just going to rewrite the code to use a split like you should have in the first place? Also, there may be a need to handle commas in the fields at some point in the future. This will entail using a module like Text::CSV. With the split code you can just replace the split with the proper parsing function from the module. With the giant substitution code you pretty much have to rewrite the whole thing. I am all for using advanced features of Perl when it makes the code clearer or more concise, but this code is longer than the split version, involves more functions (including the confusing* localtime and timelocal functions), and doesn't even do error checking on the data. On an unrelated topic, why are you using timelocal? A much better solution is to use the strftime function from the POSIX module: #!/usr/bin/perl use strict; use warnings; use POSIX; while (DATA) { s{,([^,]+),}{ my ($m, $d, $y) = $1 =~ m^([0-9]+)/([0-9]+)/([0-9]+)$ or die $. has an invalid date format; strftime ,%Y%m%d,, 0, 0, 0, $d, $m - 1, $y - 1900; }e; print; } __DATA__ 1,1/1/2009,optional,foo 2,1/2/2009,,bar 3,1/3/2009,,baz Note how the split from your code has been changed to a regex. This is because split is indiscriminate. This was good in my code because it acted as future proofing against more fields being added to the record** (which is unlikely to affect the meaning of earlier fields), but bad here because we know the expected format of the date and the chances of it not being that format and the code still being correct at some point in the future is small. * localtime pretty much only makes sense when you know the C based tm structure it came from and timelocal, besides being a word play that is too clever by half, is worse because it violates that structure***. ** also, if we wanted to throw an error because there were too few or too many fields it would be easily achieved by asking the array how many elements it held. *** http://perldoc.perl.org/Time/Local.html#Year-Value-Interpretation -- 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: Regular Expressions
Chas. Owens wrote: On Sun, Feb 8, 2009 at 03:49, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Sorry, but I fail too see how using the s/// operator to extract the date field would be so much more confusing and fragile compared to split() + join(). You are calling three functions (one of which is split) and assigning returns three times inside the replacement. Add on top of that the fact that the regex only works for the second field. Compare all of that to calling two much simpler functions, a simple substitution, and one assignment. Think you are comparing apples and oranges now. Since we don't know what kind of conversion the OP wants to do, I thought we were only discussing the date extracting part of the problem. To clarify, I rewrote my code: use Time::Local; while (DATA) { s{(?=,)(.+?)(?=,)}{ dateconvert($1) }e; print; } sub dateconvert { my ($d, $m, $y) = split /\//, shift; my $t = timelocal 0, 0, 0, $d, $m-1, $y; ($d, $m, $y) = (localtime $t)[3..5]; sprintf '%d-%02d-%02d', $y+1900, $m+1, $d; } __DATA__ TICKER,06/02/09,OPEN,HIGH,LOW,CLOSE,VOLUME,OI TICKER,07/02/09,OPEN,HIGH,LOW,CLOSE,VOLUME,OI TICKER,08/02/97,OPEN,HIGH,LOW,CLOSE,VOLUME,OI In other words, if we are to compare each others code, I believe that s{(?=,)(.+?)(?=,)}{ dateconvert($1) }e; print; ought to be compared with my @record = split /,/, $_; $record[1] = dateconvert( $record[1] ); print join ,, @record; Try to imagine what happens six months from now when you need to go back and perform a transformation on the fifth field. Are you going to extend the regex to try to capture that value? Or are you just going to rewrite the code to use a split like you should have in the first place? Didn't think about that. Maybe I will use split + join. Not a big deal, IMO. I am all for using advanced features of Perl when it makes the code clearer or more concise, but this code is longer than the split version, involves more functions (including the confusing* localtime and timelocal functions), My use of localtime and timelocal is totally unrelated to whether I use the split version or not. and doesn't even do error checking on the data. Not true. timelocal() does error checking. On an unrelated topic, why are you using timelocal? Because of its built-in error checking? ;-) Or maybe because I wanted to use its Year Value Interpretation feature. (Note that I assumed conversion from dd/mm/yy to -mm-dd, and that a date from the 90's is included in my sample data.) A much better solution is to use the strftime function from the POSIX module: Maybe. Somehow I tend to believe that date conversion code becomes more robust if you go to epoch seconds and back. Isn't that what most date and time related modules do behind the scenes, 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: Regular Expressions
On Sat, Feb 7, 2009 at 08:45, Soham Das soham...@yahoo.co.in wrote: Hi All, I am a noob in Perl and hence would like some help to what I am sure is a very easy problem. I have got a text file in csv format The format is: TICKER,DATE,OPEN,HIGH,LOW,CLOSE,VOLUME,OI Now my objective is to change the format of the date, and rename the whole file as a .csv So, my strategy is: I want to read the content between the first and second comma, take it in a variable and do the slicing and dicing and write it back. Because I need some real life practice in REGEX, how do you suggest I read the contents between the first and the second comma? snip This isn't a job for a regex; it is a job for split: my @record = split ,, $record; $record[1] =~ s{(..)/(..)/()}{$3$1$2} or die line $. has an invalid date format; print join ,, @record; You could say $record =~ s{(.*?),(..)/(..)/(),}{$1,$4$2$3,} or die line $. has an invalid date format; print $record; but the next person to maintain your code may be a little upset at you, especially in the more complicated versions of this type of substitution. -- 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: Regular Expressions
Chas. Owens wrote: This isn't a job for a regex; it is a job for split: whose first argument is a regex pattern... ;-) -- 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: Regular Expressions
On Sat, Feb 7, 2009 at 16:09, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Chas. Owens wrote: This isn't a job for a regex; it is a job for split: whose first argument is a regex pattern... ;-) snip Yes and a regex follows in the substitute, but the whole things isn't being done with a regex. Trying to do it with one regex can lead to a confusing and fragile mess. -- 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: Regular Expressions
Chas. Owens wrote: On Sat, Feb 7, 2009 at 16:09, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Chas. Owens wrote: This isn't a job for a regex; it is a job for split: whose first argument is a regex pattern... ;-) snip Yes and a regex follows in the substitute, but the whole things isn't being done with a regex. Trying to do it with one regex can lead to a confusing and fragile mess. TMTOWTDI use Time::Local; while (DATA) { s{,(.+?),}{ my ($d, $m, $y) = split /\//, $1; my $t = timelocal 0, 0, 0, $d, $m-1, $y; ($d, $m, $y) = (localtime $t)[3..5]; sprintf ',%d-%02d-%02d,', $y+1900, $m+1, $d; }e; } -- 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: Regular Expressions
Chas. Owens wrote: On Sat, Feb 7, 2009 at 08:45, Soham Das soham...@yahoo.co.in wrote: Hi All, I am a noob in Perl and hence would like some help to what I am sure is a very easy problem. I have got a text file in csv format The format is: TICKER,DATE,OPEN,HIGH,LOW,CLOSE,VOLUME,OI Now my objective is to change the format of the date, and rename the whole file as a .csv So, my strategy is: I want to read the content between the first and second comma, take it in a variable and do the slicing and dicing and write it back. Because I need some real life practice in REGEX, how do you suggest I read the contents between the first and the second comma? snip This isn't a job for a regex; it is a job for split: my @record = split ,, $record; $record[1] =~ s{(..)/(..)/()}{$3$1$2} or die line $. has an invalid date format; print join ,, @record; You could say $record =~ s{(.*?),(..)/(..)/(),}{$1,$4$2$3,} or die line $. has an invalid date format; print $record; but the next person to maintain your code may be a little upset at you, especially in the more complicated versions of this type of substitution. $record =~ s|,(..)/(..)/(),|,$3$1$2,| or die Data problem; Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regular Expressions
On Sat, Feb 7, 2009 at 19:21, Rob Dixon rob.di...@gmx.com wrote: snip $record =~ s|,(..)/(..)/(),|,$3$1$2,| or die Data problem; snip Yes, but how would you handle it if this weren't the second field? It is better to have a general solution. -- 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: Regular Expressions
On Sat, Feb 7, 2009 at 19:11, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Chas. Owens wrote: On Sat, Feb 7, 2009 at 16:09, Gunnar Hjalmarsson nore...@gunnar.cc wrote: Chas. Owens wrote: This isn't a job for a regex; it is a job for split: whose first argument is a regex pattern... ;-) snip Yes and a regex follows in the substitute, but the whole things isn't being done with a regex. Trying to do it with one regex can lead to a confusing and fragile mess. TMTOWTDI use Time::Local; while (DATA) { s{,(.+?),}{ my ($d, $m, $y) = split /\//, $1; my $t = timelocal 0, 0, 0, $d, $m-1, $y; ($d, $m, $y) = (localtime $t)[3..5]; sprintf ',%d-%02d-%02d,', $y+1900, $m+1, $d; }e; } snip And this would be the confusing, fragile mess I spoke of. -- 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: regular expressions issue
On 6/27/07, Amichai Teumim [EMAIL PROTECTED] wrote: If I use the regular expression with the grep command in terminal I get only the IPs. Here in Perl I don't get any output. The grep command uses grep's regular expressions, but Perl uses Perl's regular expressions. Alas, everybody's regular expressions are different. Perl's are usually better, of course. But the syntax is always different. @input = `cat ~/ip.txt`; I hope that this is _supposed_ to be a quick-and-dirty program. This works, although it's slower than using a filehandle would be, and it probably uses more memory. Although if you're using the tilde to open a file in the user's home directory, well, that's maybe the best way to do it. /[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){ I think in Perl that pattern might be this: /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ But do you really want to match 999.999.999.999? You don't have to. Have you heard of Regexp::Common? Regexp::Common::net seems to have what you want. /^$RE{net}{IPv4}$/ http://search.cpan.org/~abigail/Regexp-Common-2.120/lib/Regexp/Common.pm http://search.cpan.org/dist/Regexp-Common/lib/Regexp/Common/net.pm Even if you don't want to install the module to get just one pattern, you could use the pattern that it supplies, which is sure to be at least as good as anything you would write on your own. Good luck with it! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: regular expressions issue
Amichai Teumim wrote: I created a file called data.txt which contains a bunch of junk, including some IPs. I want $line to be stored in $iphttp://www.tek-tips.com/viewthread.cfm?qid=1382614page=1# . It works, except for the regular expressions which should find only IPs. If I use the regular expression with the grep command in terminal I get only the IPs. Here in Perl I don't get any output. #!/usr/bin/perl @input = `cat ~/ip.txt`; foreach $line (@input){ if($line =~ /[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){ $ip = $line; print $ip; } } Any ideas? It's breaking my head. Perl doesn't require the braces to be escaped. As it is the regex is matching literal braces in the string which don't exist. Try this: if ($line =~ /[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/) { : } and, by the way, [0-9] is more concise than [[:digit:]]. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Regular expressions
Sombody help me if i give ([a-z]+)(.*)([a-z]+) as input string output i get is $1 is 'silly' $2 is 'silly' $3 is 'silly' this is wrong according to be book i refer please somone clarify me code i used is as below This is correct. first word that matches ([a-z]+) is 'silly'. print \$1 is '$1'\n if defined $1; print \$2 is '$1'\n if defined $2; print \$3 is '$1'\n if defined $3; print \$4 is '$1'\n if defined $4; print \$5 is '$1'\n if defined $5; maybe you ment somthing like this: print \$1 is '$1'\n if defined $1; print \$2 is '$2'\n if defined $2; print \$3 is '$3'\n if defined $3; print \$4 is '$4'\n if defined $4; print \$5 is '$5'\n if defined $5; smime.p7s Description: S/MIME Cryptographic Signature
Re: regular expressions
On Apr 21, 2006, at 16:10, Bowen, Bruce wrote: In perldoc under this topic s is listed as Treat string as a single line and m as Treat string as multiples lines. If I have text that has varying spaces at the begging of each line, and I use $string =~ s/^\s+//; It will remove the spaces from in from of the first line but not any other lines. That is clear to me. However, it does not clear all of the leading spaces from all of the lines if I use $string =~ m/^\s+//; Modifiers go to the end: $string =~ s/^\s+//m; -- fxn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regular expressions
Bowen, Bruce wrote: In perldoc under this topic s is listed as Treat string as a single line and m as Treat string as multiples lines. If I have text that has varying spaces at the begging of each line, and I use $string =~ s/^\s+//; It will remove the spaces from in from of the first line but not any other lines. That is clear to me. However, it does not clear all of the leading spaces from all of the lines if I use $string =~ m/^\s+//; In fact I'm getting error message compile error. What am I missing here? perldoc perlop [snip] m/PATTERN/cgimosx ^^ ^ [snip] s/PATTERN/REPLACEMENT/egimosx ^^ ^ The /s option affects the behaviour of the . meta-character. The /m option affects the behaviour of the ^ and $ meta-characters. Assuming you have the string: my $string = one\n two\n three\nfour\n five\n; $string =~ s/.+//; Will produce the string: \n two\n three\nfour\n five\n And: $string =~ s/.+//g; Will produce the string: \n\n\n\n\n While: $string =~ s/.+//s; Will produce the string: $string =~ s/^\s+//; Will produce the string: one\n two\n three\nfour\n five\n (It isn't modified.) While: $string =~ s/^\s+//m; Will produce the string: one\ntwo\n three\nfour\n five\n (Only the first match is changed.) And: $string =~ s/^\s+//mg; Will produce the string: one\ntwo\nthree\nfour\nfive\n John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: regular expressions over multiple lines
Hi The simplest approuch would be to something like this: # @files contains the files to be processed. for my $file (@files){ my $content; local $/ = ''; # Sets the INPUT_RECORD_SEPERATOR as empty string open(IN, $file) or warn Couldn't open $file for reading: $!; $content = IN; close(IN); # Take everything that's between EXP1 and EXP2 if they exist if ($content =~ /EXP1(.+)EXP2/s){ print $1; # Contains what's between the parenthesis } } Best regards, Grétar Rimma Nayshulis wrote: Hi, I've never used perl before, but was told that it's pretty powerful for text processing. Here's a problem I'm trying to solve: I have an expression, exp1 that I need to grep for recursively over a directory structure. If I find a match, I need to look at the matching file and find another expression, exp2. Both exp1 and exp2 belong to the same pattern that always looks like this { exp2 some text some lines of text some lines of tex . exp1 more lines of text more lines of text } The goal of this exercise is to see what some text says in the files that contain exp1. I can always read all of the files recursively line by line, save each line into an array until I find exp1 and then go backwards through the array until I find exp2 and see what some text on that line is, but I was wondering if I can somehow use regular expressions over multiple lines to do this. I hope this is a clear explanation of my problem. Any insights or pointers to resourcesare greatly appreciated! Thanks! - Relax. Yahoo! Mail virus scanning helps detect nasty viruses! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Techno Boi -- was Re: Regular Expressions : Help in understanding
Oops - mean't to group reply On 7/18/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: H. Maybe techno enough to know how to download stuff his mummy and daddy wouldn't approve of?? On 7/17/05, robert johnson [EMAIL PROTECTED] wrote: so, am i to read his email as kisses and hugs from techno-boy? so... that's not techno in the sense the boy could ever figure out how to unsubscribe from a mailing list. oh wait, i'll bet the boy is techno in the sense can send kisses and hugs over AOL Instant Message. ok, i get it now. --- jm [EMAIL PROTECTED] wrote: not sure about the rest of its name but at least it got the b0y right (unless it's also gender- as well as maturity-challenged, of course) On 7/17/05, x0x_t3chn0b0y_x0x [EMAIL PROTECTED] wrote: FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response --Rob Programmers have been likened to modern day Sorcerers. Well, I'm the Mickey Mouse apprentice who spawned an infinite number of zombie broom processes, and got in big trouble when the Sorcerer came back. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- Dave All us base are belong to you. -- Dave All us base are belong to you. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions : Help in understanding
On Sun, 17 Jul 2005, atul ashpalia wrote: Can anyone help me in understanding the Example 5 in the link below w.r.t (First Match) and (All Matches) words. What link? You never posted a URL. If the example is short (a page/screen or so), can you just paste it in an email to the list, along with the URL? This isn't homework, is it? -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions : Help in understanding
FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions : Help in understanding
not sure about the rest of its name but at least it got the b0y right (unless it's also gender- as well as maturity-challenged, of course) On 7/17/05, x0x_t3chn0b0y_x0x [EMAIL PROTECTED] wrote: FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Techno Boi -- was Re: Regular Expressions : Help in understanding
so, am i to read his email as kisses and hugs from techno-boy? so... that's not techno in the sense the boy could ever figure out how to unsubscribe from a mailing list. oh wait, i'll bet the boy is techno in the sense can send kisses and hugs over AOL Instant Message. ok, i get it now. --- jm [EMAIL PROTECTED] wrote: not sure about the rest of its name but at least it got the b0y right (unless it's also gender- as well as maturity-challenged, of course) On 7/17/05, x0x_t3chn0b0y_x0x [EMAIL PROTECTED] wrote: FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response --Rob Programmers have been likened to modern day Sorcerers. Well, I'm the Mickey Mouse apprentice who spawned an infinite number of zombie broom processes, and got in big trouble when the Sorcerer came back. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions : Help in understanding
Perhaps the GMail abuse people will rid their system of his presence. Of course when your address is Joe Wilson, your not likely to actually be able to hide behind some other handle very well. p.s. spamming individual subscribers who have no admin access will not get you very far, its not like I can unsubscribe you. x0x_t3chn0b0y_x0x wrote: FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK FUCKERS REMOVE ME IF USED THE UNSUBSCRIBE IT DONT WORK -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Regular Expressions
April T.Barrett [EMAIL PROTECTED] asked: I need some tips on going about regular expression: parsing through a text file and printing all of the words that - for example - Begin and end with an G Sample text courtesy of Project Gutenberg: #!/usr/bin/perl -w while( my $line = DATA ){ while( $line =~ m/\W((g\w*?)|(\w*?g))\W/ig ){ print Line $.: $1\n; } } __DATA__ Puck's Song See you the dimpled track that runs, All hollow through the wheat? O that was where they hauled the guns That smote King Philip's fleet! See you our little mill that clacks, So busy by the brook? She has ground her corn and paid her tax Ever since Domesday Book. See you our stilly woods of oak, And the dread ditch beside? O that was where the Saxons broke, On the day that Harold died! See you the windy levels spread About the gates of Rye? O that was where the Northmen fled, When Alfred's ships came by! See you our pastures wide and lone, Where the red oxen browse? O there was a City thronged and known, Ere London boasted a house! And see you, after rain, the trace Of mound and ditch and wall? O that was a Legion's camping-place, When Caesar sailed from Gaul! And see you marks that show and fade, Like shadows on the Downs? O they are the lines the Flint Men made, To guard their wondrous towns! Trackway and Camp and City lost, Salt Marsh where now is corn; Old Wars, old Peace, old Arts that cease, And so was England born! She is not any common Earth, Water or Wood or Air, But Merlin's Isle of Gramarye, Where you and I will fare. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions
Thomas Bätzler [TB], on Wednesday, July 13, 2005 at 12:38 (+0200) made these points: that - for example - Begin and end with an G TB while( $line =~ m/\W((g\w*?)|(\w*?g))\W/ig ){ this will print OR, he wants AND, so, change this regexp to: m/\W((g)\w*\2)\W/ig -- How do you protect mail on web? I use http://www.2pu.net [Today is a good day to bribe a high--ranking official.] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions
On Jul 13, Ing. Branislav Gerzo said: Thomas Bätzler [TB], on Wednesday, July 13, 2005 at 12:38 (+0200) made these points: that - for example - Begin and end with an G TB while( $line =~ m/\W((g\w*?)|(\w*?g))\W/ig ){ this will print OR, he wants AND, so, change this regexp to: m/\W((g)\w*\2)\W/ig Except that both of these regexes fail on the string goulag, because \W has to MATCH a non-word character. Use word boundaries instead. And in your regex, Ing, there's no reason to capture 'g' and use \2 later. It's not a variable pattern, so hard-code the 'g' both times. Here's a first draft: /\b(g\w*g)\b/i That matches a word that starts with a 'g' (or a 'G', since the /i modifier makes the regex case-insensitive), then has zero or more word characters (a-zA-Z0-9_), and then a 'g' (or 'G'). Immediately before the first 'g' and after the last 'G' cannot be another word character, so while it matches grog in do you like grog?, it doesn't match the gogg in I'm wearing goggles. However, this requires the word to be at least two characters long -- the single letter 'g' as a word (if you'd permit that) fails to match, because the regex requires at least two characters. An easy workaround is: /\b(g(?:\w*g)?)\b/ which makes the .g part optional (so long as the word boundaries still match). The remaining problem is in the definition of a word. What about I got a grab-bag gift? Should grab-bag match? It doesn't currently, because the '-' is not a valid word character (that is, it isn't matched by \w). -- Jeff japhy Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? http://www.perlmonks.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Regular Expressions
Hi , Use the expression/^G.*G$/ ^ Denotes the beginning of the line $ - denotes ending of the line if u dont want case sensitivity use/^G.*G$/i i -- ignores case sensitivity Arjun Deserve before you desire -Original Message- From: April T.Barrett [ mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] Sent: Wednesday, July 13, 2005 3:49 AM To: beginners@perl.org Subject: Regular Expressions Hello: I need some tips on going about regular expression: parsing through a text file and printing all of the words that - for example - Begin and end with an G Thanks. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/ http://learn.perl.org/first-response http://learn.perl.org/first-response Confidentiality Notice The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain confidential or privileged information. If you are not the intended recipient, please notify the sender at Wipro or [EMAIL PROTECTED] immediately and destroy all copies of this message and any attachments.
Re: Regular expressions and closing HTML tags
Chris Schults wrote: I have a text string, with some HTML code, that I truncate (using substr). In some instances, I truncate off some HTML close tags. In the case of anchor tags, I do this (please tell me if there is a more elegant ay to do this!): if ($text =~ /\a href/ $text !~ /\\/a\/) {$text .= '/a';} But I realized that this doesn't work if there is more than one link, like so: Here is a href=#some text/a that got a href=#truncated Any suggestions? Chris This is why many here continually advocate the use of HTML parsing modules for this type of work. The more elegant way to do this would be to use an HTML parser and drop the regexes completely. There are a number available from CPAN, and I have found working with HTML::TokeParser::Simple to be, well, simple. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: regular expressions with and do { ?
Michael Gale wrote: ... m|output:DISK (\w+) \[(\d+) kB \((\d+)%\) free on (\S+)\]| and do { my $status = $1; my $kb_free = $2; my $pct_free = $3; my $mount= $4; push @s, [ $mount, [ blockpct, GAUGE, $pct_free ] ]; }; I do not understand the and do option. do here turns a block into an expression. The and causes the RHS to be evaluated if the LHS is true. In otherwords, if the m|| match succeeds, evaluate the stuff in the do {} block. This notation is legal, but a bit unusual. The equivalent, and more common construct would be: if (m|output:DISK (\w+) \[(\d+) kB \((\d+)%\) free on (\S+)\]|) { my $status = $1; my $kb_free = $2; my $pct_free = $3; my $mount= $4; push @s, [ $mount, [ blockpct, GAUGE, $pct_free ] ]; }; The important thing for both formats is to always test whether the pattern match succeeds before attempting to use $1, $2, etc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions - multiplelines
Another alternative: #!/usr/bin/perl use warnings; use strict; my $eol = '[\n\r\x0A\x0D]'; $/ = ''; while (DATA) { print /^(.*):$eol([^ ]*)/ ? $1/$2\n : $_; } __DATA__ $/Dev/something/something.com/blah1: default.asp userExc 26/07/04 1:42p [TEST-DEV]F:\content\blah\wwwroot\blah1 $/Dev/something/something.com/something: test.asp userExc 26/07/04 3:09p [TEST-DEV] F:\content\blah\wwwroot\something $/Dev/something/something.com/blah: Blah.inc userExc 23/07/04 11:13a [BAGEL-DEV] F:\content\blah\wwwroot\blah $/Dev/something/something.com/blah/blah/20GB: Something-were.htm userExc 23/07/04 11:24a [TEST-DEV]F:\content\blah\wwwroot\blah\blah\20GB -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions - multiplelines
Hi Roman, Roman wrote: use strict; my $file = shift; my $line; open(IN, $file) || die $!; while($line =IN) { $line =~ /(\$\S+):.(\S+)/is; print match1: $1; match2: $2 \n; } I would redefine the record separator for your while loop to process each of the four records in your example as a whole unit, instead of going line-by-line. You can then write a regular expression to capture the path and the file name. #!/usr/bin/perl use strict; use warnings; local $/ = \n\n; # record separator = two consecutive line breaks while () { m/ ^ # beginning of line ( # start capture [^:]+ # capture all non-colon chars ) : # colon \s+ # space chars, including line break ( # second capture [^.]+ # one or more non-period chars \. # a period \S+ # one or more non-space chars ) /x; # m/^([^:]+):\s+([^.]+\.\S+)/; print $1,'/',$2,\n; } Using shift to set $file to the file name and explicitly defining $line are not necessary. This script does the same thing as yours. This is Perl's way of making things easier for you. ;) I would also suggest you 'use warnings' when writing code. It'll catch a lot of errors for you, such as a 'Use of uninitialized value in concatenation (.) or string' in your code. You can get a (partial) explanation by adding a 'use diagnostics' line to the beginning of your program. Best, Damon -- Damon Allen DAVISON http://www.allolex.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions - multiplelines
Roman Hanousek wrote: Hi Hello, I have a txt file that contains the following info: Text start here $/Dev/something/something.com/blah1: default.asp userExc 26/07/04 1:42p [TEST-DEV]F:\content\blah\wwwroot\blah1 $/Dev/something/something.com/something: test.asp userExc 26/07/04 3:09p [TEST-DEV] F:\content\blah\wwwroot\something $/Dev/something/something.com/blah: Blah.inc userExc 23/07/04 11:13a [BAGEL-DEV] F:\content\blah\wwwroot\blah $/Dev/something/something.com/blah/blah/20GB: Something-were.htm userExc 23/07/04 11:24a [TEST-DEV]F:\content\blah\wwwroot\blah\blah\20GB Text end here The result I am trying to get is the this: $/Dev/something/something.com/blah1/default.asp $/Dev/something/something.com/something/test.asp $/Dev/something/something.com/blah/Blah.inc $/Dev/something/something.com/blah/blah/20GB/Something-were.htm I been trying to various combinations of the below. If i could work out how to match the file name on the line below. Perl code start -- use strict; my $file = shift; my $line; open(IN, $file) || die $!; while($line =IN) { $line =~ /(\$\S+):.(\S+)/is; print match1: $1; match2: $2 \n; } Perl code end-- This works for me with the data provided: #!/usr/bin/perl use warnings; use strict; my $file = shift or die usage: $0 filename\n; open IN, '', $file or die Cannot open $file: $!; $/ = ''; # set $/ to paragraph mode while ( IN ) { s!:\n!/!; print /^(\S+)/, \n; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular expressions
On Dec 17, 2003, at 9:59 AM, Hemond, Steve wrote: Hi again, I want to make a search on two words. If 'one two' is found, it is okay. If 'one' is found, it is incorrect. If 'two' is found, it is also incorrect. I want the search to return me occurences of 'one two' found together. I am searching this way : if ($text =~ /one two/) What I am doing wrong? Nothing that I can see. Although, /\bone two\b/ is probably slightly better. What's the problem? James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : Regular expressions
Actually, I have to test two conditions to get into a block, If the strings found are either one two or two three, go ahead. if ($text =~ /one two/ or /two three/) Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: James Edward Gray II [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 11:05 AM To: Hemond, Steve Cc: [EMAIL PROTECTED] Subject: Re: Regular expressions On Dec 17, 2003, at 9:59 AM, Hemond, Steve wrote: Hi again, I want to make a search on two words. If 'one two' is found, it is okay. If 'one' is found, it is incorrect. If 'two' is found, it is also incorrect. I want the search to return me occurences of 'one two' found together. I am searching this way : if ($text =~ /one two/) What I am doing wrong? Nothing that I can see. Although, /\bone two\b/ is probably slightly better. What's the problem? James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : Regular expressions
On Dec 17, Hemond, Steve said: Actually, I have to test two conditions to get into a block, If the strings found are either one two or two three, go ahead. if ($text =~ /one two/ or /two three/) That code is the same as if (($text =~ /one two/) or ($_ =~ /two three/)) { ... } You want either if ($text =~ /one two/ or $text =~ /two three/) { ... } or if ($text =~ /one two|two three/) { ... } -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : Regular expressions
On Dec 17, 2003, at 10:09 AM, Hemond, Steve wrote: Actually, I have to test two conditions to get into a block, If the strings found are either one two or two three, go ahead. if ($text =~ /one two/ or /two three/) Now that is a mistake. ;) if ($text =~ /\bone two\b/ || $text =~ /\btwo three\b/) { James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular expressions
On Dec 17, 2003, at 7:59 AM, Hemond, Steve wrote: [..] I am searching this way : if ($text =~ /one two/) What I am doing wrong? ok, I bite, what is the problem? given #!/usr/bin/perl -w use strict; while ( DATA ) { my $text = $_; if ($text =~ /one two/) { print line is ok:\n\t$text; } else { print WRONG!\n\t$text; } } __DATA__ If 'one two' is found, it is okay. If 'one' is found, it is incorrect. If 'two' is found, it is also incorrect. we get line is ok: If 'one two' is found, it is okay. WRONG! If 'one' is found, it is incorrect. WRONG! If 'two' is found, it is also incorrect. What Problem? ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : Regular expressions
Okay, here`s the real problem, # ps -efA |grep dispatch cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im My script passes each process and when it finds dispatch genie it holds its data in a hash table. As you can see, dispatch genie is found in these two columns. if ($cmd =~ /dispatch genie/) { That returns absolutely nothing. Why? Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: drieux [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 11:24 AM To: Perl Perl Subject: Re: Regular expressions On Dec 17, 2003, at 7:59 AM, Hemond, Steve wrote: [..] I am searching this way : if ($text =~ /one two/) What I am doing wrong? ok, I bite, what is the problem? given #!/usr/bin/perl -w use strict; while ( DATA ) { my $text = $_; if ($text =~ /one two/) { print line is ok:\n\t$text; } else { print WRONG!\n\t$text; } } __DATA__ If 'one two' is found, it is okay. If 'one' is found, it is incorrect. If 'two' is found, it is also incorrect. we get line is ok: If 'one two' is found, it is okay. WRONG! If 'one' is found, it is incorrect. WRONG! If 'two' is found, it is also incorrect. What Problem? ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : Regular expressions
On Dec 17, 2003, at 10:29 AM, Hemond, Steve wrote: Okay, here`s the real problem, # ps -efA |grep dispatch cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im My script passes each process and when it finds dispatch genie it holds its data in a hash table. As you can see, dispatch genie is found in these two columns. if ($cmd =~ /dispatch genie/) { That returns absolutely nothing. Why? returns nothing is a little confusing. The test above should evaluate to true, if $cmd contains 'dispatch genie' and whatever is inside the if block should be executed. If you're saying the if block isn't being executed, well $cmd probably doesn't contain what you think it does then. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: RE : Regular expressions
Hemond, Steve wrote: Okay, here`s the real problem, # ps -efA |grep dispatch cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im My script passes each process and when it finds dispatch genie it holds its data in a hash table. As you can see, dispatch genie is found in these two columns. if ($cmd =~ /dispatch genie/) { That returns absolutely nothing. Why? Again, that regexp works just fine. (If you're setting $cmd properly...) Something else must be wrong that you're not sending in your email requests. Send the actual code. #!/usr/bin/perl -w use warnings; use strict; while (DATA) { chomp; (/dispatch genie/) ? print Hit\n : print Miss\n; } __DATA__ cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : RE : Regular expressions
I think I begin to understand... I begin by fetching the results of the ps -efA command and split it into many variables ($uid, $pid, etc.) open(PS, ps -efA|); while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; if ($cmd =~ /dispatch genie/) { $infos{$pid}{'uid'} = $uid; $infos{$pid}{'ppid'} = $ppid; $infos{$pid}{'c'} = $c; $infos{$pid}{'stime'} = $stime; $infos{$pid}{'tty'} = $tty; $infos{$pid}{'time'} = $time; $infos{$pid}{'cmd'} = $cmd; I thought $cmd, as the last variable for the split command, would contain the next word and also the rest of the line, which it does not. It seems to take only the next word and do nothing with the rest of the line. So $cmd contains /prog/gena/8.1.1/bin/dispatch NOT /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: Ed Christian [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 11:40 AM To: Hemond, Steve Cc: [EMAIL PROTECTED] Subject: RE: RE : Regular expressions Hemond, Steve wrote: Okay, here`s the real problem, # ps -efA |grep dispatch cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im My script passes each process and when it finds dispatch genie it holds its data in a hash table. As you can see, dispatch genie is found in these two columns. if ($cmd =~ /dispatch genie/) { That returns absolutely nothing. Why? Again, that regexp works just fine. (If you're setting $cmd properly...) Something else must be wrong that you're not sending in your email requests. Send the actual code. #!/usr/bin/perl -w use warnings; use strict; while (DATA) { chomp; (/dispatch genie/) ? print Hit\n : print Miss\n; } __DATA__ cspenard 33958 45716 0 09:08:05 pts/8 0:00 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im msirois 37212 9842 0 08:41:17 pts/1 0:04 /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : Regular expressions
On Dec 17, 2003, at 8:47 AM, Hemond, Steve wrote: [..] open(PS, ps -efA|); while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; [..] forgive me if I presume that you are running on some version of Linux? ( because by BSD boxes do not like the A and my Solaris box would get what you are looking for ) hence meatbop: 53:] ps -efA | grep httpd.conf drieux 11069 10988 0 10:08 pts/000:00:00 grep httpd.conf meatbop: 54:] ps -efAww | grep httpd.conf root 1644 1 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1645 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1646 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1647 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1648 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1649 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf drieux 11071 11031 0 10:08 pts/100:00:00 grep httpd.conf meatbop: 55:] it is possible that you are getting bitten by the classic problem of the 'ps output' being truncated to the 'terminal size' since you did not expressly assert that you wanted to 'go big'. Hence the problem is NOT with the RegEx, but with limitations of what is actually being put into $cmd based upon the input from the popen() command as invoked. HTH. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : RE : RE : Regular expressions
I am issuing this command on an Aix box and running allright :-) Forgive my curiosity.. are you running Solaris on a x86 box? Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: drieux [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 1:14 PM To: Perl Perl Subject: Re: RE : RE : Regular expressions On Dec 17, 2003, at 8:47 AM, Hemond, Steve wrote: [..] open(PS, ps -efA|); while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; [..] forgive me if I presume that you are running on some version of Linux? ( because by BSD boxes do not like the A and my Solaris box would get what you are looking for ) hence meatbop: 53:] ps -efA | grep httpd.conf drieux 11069 10988 0 10:08 pts/000:00:00 grep httpd.conf meatbop: 54:] ps -efAww | grep httpd.conf root 1644 1 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1645 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1646 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1647 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1648 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf hqadmin 1649 1644 0 Nov10 ?00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf drieux 11071 11031 0 10:08 pts/100:00:00 grep httpd.conf meatbop: 55:] it is possible that you are getting bitten by the classic problem of the 'ps output' being truncated to the 'terminal size' since you did not expressly assert that you wanted to 'go big'. Hence the problem is NOT with the RegEx, but with limitations of what is actually being put into $cmd based upon the input from the popen() command as invoked. HTH. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : RE : Regular expressions
On Dec 17, 2003, at 10:16 AM, Hemond, Steve wrote: I am issuing this command on an Aix box and running allright :-) interesting, let's do a quick piece of test code open(PS, ps -efA|) or die unable to open ps command\n:$!; while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; print $pid - $cmd\n; } close(PS); that will at least get us to what is going on with the actual $cmd data itself. Forgive my curiosity.. Curiosity Killed The Cat you know... But the cat came back, the very next day, thought he was a gonner but cat came back... - old american folk song. are you running Solaris on a x86 box? Solaris on Sparc and x86 FreeBsd, some variations on linux, my desk top tho is OSX. Haven't been on an AIX box in a while. Loved Unicos. But real Men are not afraid to toggle it into a PDP-8 or an AN/Ukky-20 in battleship grey, or ... ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : RE : RE : RE : Regular expressions
Here is a sample of what your piece of code returns on my Aix box. 44520 - /prog/gena/8.1.1/bin/dispatch 44650 - reproject 45176 - aioserver 45432 - aioserver 45724 - -ksh 46002 - /bin/bsh 46232 - /usr/dt/bin/dtterm 46584 - /usr/bin/ksh 46820 - /usr/dt/bin/ttsession 47060 - /bin/bsh 47304 - /usr/dt/bin/dtlogin 47396 - /usr/dt/bin/dtterm 47722 - dtfile 47942 - /usr/dt/bin/dtsession 48272 - dtfile 48568 - ora_cjq0_gist 48758 - gxtrackd 49032 - dtwm 49330 - /usr/lib/lpd/pio/etc/piohpnpf 49592 - bsh 49672 - /usr/dt/bin/dtterm 50170 - /usr/dt/bin/dtlogin 50400 - dtterm 50604 - dispscript 50844 - genie 51096 - /prog/gena/8.1.1/bin/dispatch 51348 - gmap 51676 - reproject 51874 - gxtrackd 52086 - ps 52378 - perl 52646 - sh 53164 - /usr/bin/ksh 53432 - /bin/bsh Like $cmd will stop at the end of the word, not the end of the rest of the line. Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: drieux [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 1:24 PM To: Perl Perl Subject: Re: RE : RE : RE : Regular expressions On Dec 17, 2003, at 10:16 AM, Hemond, Steve wrote: I am issuing this command on an Aix box and running allright :-) interesting, let's do a quick piece of test code open(PS, ps -efA|) or die unable to open ps command\n:$!; while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; print $pid - $cmd\n; } close(PS); that will at least get us to what is going on with the actual $cmd data itself. Forgive my curiosity.. Curiosity Killed The Cat you know... But the cat came back, the very next day, thought he was a gonner but cat came back... - old american folk song. are you running Solaris on a x86 box? Solaris on Sparc and x86 FreeBsd, some variations on linux, my desk top tho is OSX. Haven't been on an AIX box in a while. Loved Unicos. But real Men are not afraid to toggle it into a PDP-8 or an AN/Ukky-20 in battleship grey, or ... ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : RE : Regular expressions
On Dec 17, 2003, at 10:24 AM, drieux wrote: open(PS, ps -efA|) or die unable to open ps command\n:$!; while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; print $pid - $cmd\n; } close(PS); Tell me not to make my coffee with last night's bong water! if split is splitting on the default blank space, root $1 1644 $2 1 $3 0 $4 Nov10 $5 ? $6 00:00:00 $7 /usr/sbin/httpd2-prefork $8 -f /etc/apache2/httpd.conf what we want is the saner process of ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split(/\s+/,$_,8); eg: sed 's/^/### /' junk.plx ### #!/usr/bin/perl -w ### use strict; ### ### open(PS, ps -efAww | grep http|); ### while(PS) ### { ### # my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; ### # print $pid - $cmd\n; ### my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split(/\s+/,$_,8); ### print Better $pid - $cmd\n; ### print \t$uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd\n; ### ### } My Apology! ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : Regular expressions
I think I begin to understand... I begin by fetching the results of the ps -efA command and split it into many variables ($uid, $pid, etc.) open(PS, ps -efA|); while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; if ($cmd =~ /dispatch genie/) { $infos{$pid}{'uid'} = $uid; $infos{$pid}{'ppid'} = $ppid; $infos{$pid}{'c'} = $c; $infos{$pid}{'stime'} = $stime; $infos{$pid}{'tty'} = $tty; $infos{$pid}{'time'} = $time; $infos{$pid}{'cmd'} = $cmd; I thought $cmd, as the last variable for the split command, would contain the next word and also the rest of the line, which it does not. It seems to take only the next word and do nothing with the rest of the line. So $cmd contains /prog/gena/8.1.1/bin/dispatch NOT /prog/gena/8.1.1/bin/dispatch genie -u /prog/gena/impress/gui/im Ah! And now we have come full circle back to Why you should use a module to handle this type of code rather than re-inventing the wheel! You don't risk breaking the spokes of the wheel while fixing the pedals if you don't start out by reinventing the wheel to begin with... http://danconia.org -- Boycott the Sugar Bowl! You couldn't pay me to watch that game. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : RE : RE : RE : Regular expressions
No worries :-) It works now, thanks a lot :-) Best regards, Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: drieux [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 1:38 PM To: Perl Perl Subject: Re: RE : RE : RE : Regular expressions On Dec 17, 2003, at 10:24 AM, drieux wrote: open(PS, ps -efA|) or die unable to open ps command\n:$!; while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; print $pid - $cmd\n; } close(PS); Tell me not to make my coffee with last night's bong water! if split is splitting on the default blank space, root $1 1644 $2 1 $3 0 $4 Nov10 $5 ? $6 00:00:00 $7 /usr/sbin/httpd2-prefork $8 -f /etc/apache2/httpd.conf what we want is the saner process of ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split(/\s+/,$_,8); eg: sed 's/^/### /' junk.plx ### #!/usr/bin/perl -w ### use strict; ### ### open(PS, ps -efAww | grep http|); ### while(PS) ### { ### # my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; ### # print $pid - $cmd\n; ### my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split(/\s+/,$_,8); ### print Better $pid - $cmd\n; ### print \t$uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd\n; ### ### } My Apology! ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : Regular expressions
On Dec 17, 2003, at 10:40 AM, Wiggins d Anconia wrote: [..] Ah! And now we have come full circle back to Why you should use a module to handle this type of code rather than re-inventing the wheel! [..] technically I agree with you. In terms of code re-use and code maintainability. So it sorta depends upon what the 'real goal' is all about... There is also the minor point, that some times doing silly things that are better covered in an already established module will help folks 'get it' about what they are doing and how they can do things. In this case steve Thought that his problem was with his lack of knowledge about 'regular expression' and how to do that. When his issue was with how to effectively use split() - one of the variants that he could have used of course would have been my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd,@args) = split; But that would have meant dealing with @args problem where he needed to find TWO things in an array in a given order, rather than merely regExing a longer scalar. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE : RE : RE : Regular expressions
drieux is right about me being exploring Perl. In fact, that is a good exercice to play with regular expressions and data types as I had to build a hash of hashes to do the thing. However, I wouldn't let a script in that stat if I knew of a better/quicker/shorter method. I will then have to improve my script soon. What would be the best way to put values returned by the ps command you just mentionned in variables? Thanks again for your great help. P.S : I look like the typical lazy guy who don`t even read and try by himself. This is because I am at work, and overloaded with other things, and as you know bosses, I made the mistake to tell him that I would do that little thing in Perl, he know thinks that I am a Perl guru! I've started reading a Perl5 book by the evenings, so, I do my homework in some way :-) I appreciate your help. Thanks again, Best regards, Steve Hemond Programmeur Analyste / Analyst Programmer Smurfit-Stone, Ressources Forestières La Tuque, P.Q. Tel.: (819) 676-8100 X2833 [EMAIL PROTECTED] -Original Message- From: drieux [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 2:04 PM To: Perl Perl Subject: Re: RE : RE : Regular expressions On Dec 17, 2003, at 10:40 AM, Wiggins d Anconia wrote: [..] Ah! And now we have come full circle back to Why you should use a module to handle this type of code rather than re-inventing the wheel! [..] technically I agree with you. In terms of code re-use and code maintainability. So it sorta depends upon what the 'real goal' is all about... There is also the minor point, that some times doing silly things that are better covered in an already established module will help folks 'get it' about what they are doing and how they can do things. In this case steve Thought that his problem was with his lack of knowledge about 'regular expression' and how to do that. When his issue was with how to effectively use split() - one of the variants that he could have used of course would have been my ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd,@args) = split; But that would have meant dealing with @args problem where he needed to find TWO things in an array in a given order, rather than merely regExing a longer scalar. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: RE : RE : Regular expressions
Steve Hemond wrote: I think I begin to understand... I begin by fetching the results of the ps -efA command and split it into many variables ($uid, $pid, etc.) open(PS, ps -efA|); You should _ALWAYS_ verify that the file opened correctly! while (PS) { ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split; Ah yes, more fun with split. :-) Your line above is interpreted by perl as: ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split ' ', $_, 9; Where the ninth field is discarded. You want to tell split that you want exactly eight fields. ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split ' ', $_, 8; You are also going to have to use chomp because $cmd will have a trailing newline. chomp; ($uid,$pid,$ppid,$c,$stime,$tty,$time,$cmd) = split ' ', $_, 8; if ($cmd =~ /dispatch genie/) { $infos{$pid}{'uid'} = $uid; $infos{$pid}{'ppid'} = $ppid; $infos{$pid}{'c'} = $c; $infos{$pid}{'stime'} = $stime; $infos{$pid}{'tty'} = $tty; $infos{$pid}{'time'} = $time; $infos{$pid}{'cmd'} = $cmd; open PS, 'ps -efA |' or die Cannot open pipe from ps: $!; while ( PS ) { next unless /dispatch genie/; chomp; my ( $uid, $pid, $ppid, $c, $stime, $tty, $time, $cmd ) = split ' ', $_, 8; @{ $infos{ $pid } }{ qw/ uid ppid c stime tty time cmd / } = ( $uid, $ppid, $c, $stime, $tty, $time, $cmd ); } close PS or die Cannot close pipe from ps: $!; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Regular Expressions and the sub command
It was Monday, October 06, 2003 when Trina Espinoza took the soap box, saying: : I am running this command on a $file, : s/[A-Z^_]*_Leq/\n[A-Z^_]*_Leq/g; : : : but the output I get takes the regular expression literally, so I get: : [A-Z^_]*_Leq : [A-Z^_]*_Leq : [A-Z^_]*_Leq : : What I would like to get is something like this: : : AB_Leq : What can I do to prevent it from taking A-Z^_]*_ literally??? The replacement portion of a regular expression can't contain a regular expression itself, as you've noticed. What you need to do is capture what you've matched so you can use it in the replacement. This is done using parens to capture. s/(foo)/.../g; Now that we've captured 'foo', we can access it from the variable called $1 in our replacement. s/(foo)_bar/$1/g; If you were to capture two parts of your regular expression you'd use $1 and $2. Casey West -- Heavier-than-air flying machines are impossible. -- Lord Kelvin, president, Royal Society, 1895. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions and the sub command
Thanks for the help! This worked perfectly! -T From: Casey West [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Trina Espinoza [EMAIL PROTECTED] CC: [EMAIL PROTECTED] Subject: Re: Regular Expressions and the sub command Date: Mon, 6 Oct 2003 13:33:25 -0400 It was Monday, October 06, 2003 when Trina Espinoza took the soap box, saying: : I am running this command on a $file, : s/[A-Z^_]*_Leq/\n[A-Z^_]*_Leq/g; : : : but the output I get takes the regular expression literally, so I get: : [A-Z^_]*_Leq : [A-Z^_]*_Leq : [A-Z^_]*_Leq : : What I would like to get is something like this: : : AB_Leq : What can I do to prevent it from taking A-Z^_]*_ literally??? The replacement portion of a regular expression can't contain a regular expression itself, as you've noticed. What you need to do is capture what you've matched so you can use it in the replacement. This is done using parens to capture. s/(foo)/.../g; Now that we've captured 'foo', we can access it from the variable called $1 in our replacement. s/(foo)_bar/$1/g; If you were to capture two parts of your regular expression you'd use $1 and $2. Casey West -- Heavier-than-air flying machines are impossible. -- Lord Kelvin, president, Royal Society, 1895. _ High-speed Internet access as low as $29.95/month (depending on the local service providers in your area). Click here. https://broadband.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
Saurabh Singhvi wrote: well i was trying to understand the regular expressions in perl when i came across STDIN Please recognize this as the coincidence it is. Regular expressions and filehandles are two different subjects. If you are somehow linking the particular built-in filehandle with the regular epression tools, you should break the linkage, and study each on its own. i tried my best but i havent been able to get the slightest idea on how the input thing works. The thing that works is not the filehandle itself, but the getline operator . Placing the filehandle STDIN within the angle barckets--in a context where the expreesion is an rvalue in a scalar assignment, tells the operator to act on STDIN. Likewise, when taken in an array or list context, the operator gets all lines/records from the file and pushes each into the list on the left of the assignment. The editor i use is DzSoft. Was this editor designed for Perl? I would recommend using a simple code editor that offers auto-indent and Unix line endings. but without code-sensitive features. Your best bet is to take the error and warnings messages directly from the command-line, since an intermediary application may simply add another layer of confusion And it shows something like get and something else below like script something somethings will seldom lead to progess in debugging. I don't mean to be picky here, but programming is a preisise art, where each character is in the code for a specific, and hopefully well-tought-out reason. Likewise, debugging messages arte delivered in as exact a form as logically possible. I put it this way because the interpreter generally issues an error only when the meaning or intent of code is impossible to decipher. When you use strict, you essentially tell the interpreter to do a little ;ess mind-reading, and to tell you when your instructions are too vague to be dependable. with blank space. Any hopes 4 me?? :-( Sure. use strict; Pay attention to the exact wording of messages returned by the interpreter. Don't deal with thingies, gizmos, or whatnots. Learn specific program structures and constructs, and respect the material. If you have a true passion for creating process, and you are willing to put serous and careful work into it, you can learn to program. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
On Tue, 3 Jun 2003 22:38:47 -0700 (PDT), [EMAIL PROTECTED] (Saurabh Singhvi) wrote: well i was trying to understand the regular expressions in perl when i came across STDIN i tried my best but i havent been able to get the slightest idea on how the input thing works. The editor i use is DzSoft. And it shows something like get and something else below like script something with blank space. Any hopes 4 me?? :-( Well i hope for me anyways ;) Help please Thanks in advamce :) All programs have 3 default input-output filehandles, they are STDIN, STDOUT, and STDERR. For Standard Input, Standard Output, and Standard Error. They default to: STDIN - keyboard input STDOUT - your screen STDERR - your screen They are used so often, that alot of short-cuts have evolved, so you don't always have to write them out. Run this and watch what it does. #!/usr/bin/perl while(){print} without the shortcuts, it would look like this: while(STDIN){print STDOUT} -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
Zentara wrote: On Tue, 3 Jun 2003 22:38:47 -0700 (PDT), [EMAIL PROTECTED] (Saurabh Singhvi) wrote: well i was trying to understand the regular expressions in perl when i came across STDIN i tried my best but i havent been able to get the slightest idea on how the input thing works. The editor i use is DzSoft. And it shows something like get and something else below like script something with blank space. Any hopes 4 me?? :-( Well i hope for me anyways ;) Help please Thanks in advamce :) All programs have 3 default input-output filehandles, ^ 5 they are STDIN, STDOUT, and STDERR. ARGV, ARGVOUT For Standard Input, Standard Output, and Standard Error. They default to: STDIN - keyboard input STDOUT - your screen STDERR - your screen They are used so often, that alot of short-cuts have evolved, so you don't always have to write them out. Run this and watch what it does. #!/usr/bin/perl while(){print} without the shortcuts, it would look like this: while(STDIN){print STDOUT} is not exactly the same as STDIN it is closer to ARGV. perldoc perlsyn perldoc perlvar John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
John W. Krahn wrote: Zentara wrote: All programs have 3 default input-output filehandles, ^ 5 they are STDIN, STDOUT, and STDERR. ARGV, ARGVOUT Or eight if you include stdin, stdout and stderr. :-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
Saurabh Singhvi wrote at Tue, 03 Jun 2003 22:38:47 -0700: well i was trying to understand the regular expressions in perl when i came across STDIN i tried my best but i havent been able to get the slightest idea on how the input thing works. The editor i use is DzSoft. And it shows something like get and something else below like script something with blank space. What have you tried so far (code)? What's your exact question? Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions http error code
Hi Derek. Derek Romeyn wrote: Using your idea I ended up with data like this. Which is odd because the database should only include 400 and 500 type errors. [snip] 404 24.54.175.153 - - [11/Mar/2003:07:48:37 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https:// 370 209.91.198.57 - - [11/Mar/2003:07:48:24 -0800] GET /e/t/search/aaa?qmenu=2sym=dyn, intc HTTP/1.0 400 370 526 66.196.65.24 - - [11/Mar/2003:07:54:32 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/5.0 (Slur 178 167.127.163.141 - isklvjyy [11/Mar/2003:08:02:46 -0800] GET /e/t/aaa HTTP/1.1 500 178 - Mozilla/4.0 (compatible 404 68.39.167.38 - - [11/Mar/2003:08:06:34 -0800] GET /e/t/aaa/img/spacer.gif HTTP/1.1 404 0 https://us.etrade.com/e/ 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en The 404's were right but the rest took the second group of numbers instead of the needed first. This is how my code looked: my $code,$msg; foreach (@RAW_DATA) { $code = $1 if m|HTTP.*\s+(\d{3})|g; Here's your problem. You're searching for 'HTTP', followed by any number of any character, followed by one or more whitespace characters and three digits. Because the '.*' will eat up as much as it can, the captured digits will be the /last/ occurrence of three digits following a space. If you change '.*' into '.*?' it will match as few characters as possible and you'll get the three digits you want. Also, do you need the /g modifier on this search? I don't think it can make any difference in this context. I'd recommend using /x though so that you can lay it out a little more visibly. ($timestamp, $msg) = split(/\t/); I'm not clear from your data which fields you're extracting, but I assume this split works as you haven't said otherwise. if (!$code) { print NEXT\n; next; } Surely you really want to 'next' if the initial match fails? print $code\t$msg\n; $code = 0; } I did manage to get a version of George's to work. Still interested in trying all variations though. The following corrects all my points above. Use it if you like it. HTH, Rob foreach (@RAW_DATA) { unless ( m| HTTP.*? \s+ (\d{3}) |x ) { print NEXT\n; next; } my $code = $1; my ($timestamp, $msg) = split(/\t/); print $code\t$msg\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
Derek Romeyn wrote: K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? top posting is bad. don't do that. respond below, or interleaved with the post you are replying to. while (IN) { next unless (my $file, $code) = m|GET\s+([/.\w\d]+)\s+HTTP/[\d.]+\s+(\d+)\s+| print $file: $code\n; } try this one on for size as a start. Does a little bit more than you asked for but you might find that useful too. ;) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
Derek Romeyn wrote: Using your idea I ended up with data like this. Which is odd because the database should only include 400 and 500 type errors. 176 404 370 157 404 370 526 178 176 404 526 526 So I went ahead and modified it to print the code and the dataline and got this: And got this: 404 24.54.175.153 - - [11/Mar/2003:07:48:37 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https:// 370 209.91.198.57 - - [11/Mar/2003:07:48:24 -0800] GET /e/t/search/aaa?qmenu=2sym=dyn, intc HTTP/1.0 400 370 526 66.196.65.24 - - [11/Mar/2003:07:54:32 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/5.0 (Slur 178 167.127.163.141 - isklvjyy [11/Mar/2003:08:02:46 -0800] GET /e/t/aaa HTTP/1.1 500 178 - Mozilla/4.0 (compatible 404 68.39.167.38 - - [11/Mar/2003:08:06:34 -0800] GET /e/t/aaa/img/spacer.gif HTTP/1.1 404 0 https://us.etrade.com/e/ 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en The 404's were right but the rest took the second group of numbers instead of the needed first. [snip] I did manage to get a version of George's to work. Still interested in trying all variations though. if you are interested in getting the status code, the following should work: #!/usr/bin/perl -w use strict; while(){ m# HTTP / \d \. \d . \s (.+?) \s #x print $1\n; } __END__ david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions http error code
R. Joseph Newton wrote: ... an integer, followed by a *backslash*. # HTTP\d+\/ There will be something on the other side of the *backslash*, to identify subversion. Let's not count baskslash? Did somebody say backslash? Huh? I didn't hear anything. | 8-O ) Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions http error code
On Wed, 12 Mar 2003, Romeyn, Derek wrote: I have a DB that records HTTP log data like this. I'm horrible at regular expressions and could use some real help in pulling out the HTTP error code from lines that look like this. 65.248.129.126 - xm1721 [11/Mar/2003:05:41:35 -0800] POST /applogic/brpulseappletinfonew HTTP/1.1 500 16 7 - 12.235.196.99 - - [11/Mar/2003:05:55:35 -0800] GET /.com HTTP/1.1 500 184 - Mozilla/4.0 (compatible; MSIE 6.0; Win 12.229.35.165 - - [12/Mar/2003:04:34:11 -0800] GET /e/t/applogic/spacer.gif HTTP/1.1 404 0 https://www.here.com/t 24.98.188.254 - - [12/Mar/2003:04:43:19 -0800] GET /e/t/kc/images/liquidation_rights.gif HTTP/1.1 404 0 https://www.h 207.5.195.98 - - [12/Mar/2003:04:56:32 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https://www.here.com/t I'm guessing I need the three digits that are located on each line between 'HTTP/\d.\d ' and another ' ' but not sure how to do that. You've almost done it yourself. I've never really worked with HTTP log data, but from the lines that you've given, I would say: my ($error_code); if ($ln =~ / HTTP\/\d\.\d\ (\d+)/) { ($error_code) = $1; } bye George P. Thanks much, Derek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? -Original Message- From: George P. [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 12, 2003 9:48 AM To: Romeyn, Derek Cc: '[EMAIL PROTECTED]' Subject: Re: Regular Expressions http error code On Wed, 12 Mar 2003, Romeyn, Derek wrote: I have a DB that records HTTP log data like this. I'm horrible at regular expressions and could use some real help in pulling out the HTTP error code from lines that look like this. 65.248.129.126 - xm1721 [11/Mar/2003:05:41:35 -0800] POST /applogic/brpulseappletinfonew HTTP/1.1 500 16 7 - 12.235.196.99 - - [11/Mar/2003:05:55:35 -0800] GET /.com HTTP/1.1 500 184 - Mozilla/4.0 (compatible; MSIE 6.0; Win 12.229.35.165 - - [12/Mar/2003:04:34:11 -0800] GET /e/t/applogic/spacer.gif HTTP/1.1 404 0 https://www.here.com/t 24.98.188.254 - - [12/Mar/2003:04:43:19 -0800] GET /e/t/kc/images/liquidation_rights.gif HTTP/1.1 404 0 https://www.h 207.5.195.98 - - [12/Mar/2003:04:56:32 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https://www.here.com/t I'm guessing I need the three digits that are located on each line between 'HTTP/\d.\d ' and another ' ' but not sure how to do that. You've almost done it yourself. I've never really worked with HTTP log data, but from the lines that you've given, I would say: my ($error_code); if ($ln =~ / HTTP\/\d\.\d\ (\d+)/) { ($error_code) = $1; } bye George P. Thanks much, Derek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? I had the same problem and someone pointed out that the paranthese is returned in list context, so :: my($newcode) = $code =~ / HTTP\/\d\.\d\ (\d+)/; if(!$newcode) ... Understanding list contewxt is very helpful DMuey -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
On Wed, 12 Mar 2003, Romeyn, Derek wrote: K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? You're not capturing the correct string. Here's a code snippet I just tried on an Apache log that worked (assuming you have an open file handle): while(LOG) { print $1\n if m|HTTP.*\s+(\d{3})|g'; } $1 contains the matched string inside the parens (\d{3}). -- Brett http://www.chapelperilous.net/ We don't know who it was that discovered water, but we're pretty sure that it wasn't a fish. -- Marshall McLuhan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
On Wed, 12 Mar 2003, Romeyn, Derek wrote: K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? I'm assuming that there is a while loop surrounding the above code, otherwise it wouldn't print NEXT more than once. When you say $code =~ / HTTP\/\d\.\d\ (\d+)/; $1 will contain the value captured by (\d+) Therefore, you have to check in $1 A better way to do this would be, if ($code =~ / HTTP\/\d\.\d\ (\d+)/) { print The code is $1\n; } else { print NEXT\n; next; } bye, George P. -Original Message- From: George P. [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 12, 2003 9:48 AM To: Romeyn, Derek Cc: '[EMAIL PROTECTED]' Subject: Re: Regular Expressions http error code On Wed, 12 Mar 2003, Romeyn, Derek wrote: I have a DB that records HTTP log data like this. I'm horrible at regular expressions and could use some real help in pulling out the HTTP error code from lines that look like this. 65.248.129.126 - xm1721 [11/Mar/2003:05:41:35 -0800] POST /applogic/brpulseappletinfonew HTTP/1.1 500 16 7 - 12.235.196.99 - - [11/Mar/2003:05:55:35 -0800] GET /.com HTTP/1.1 500 184 - Mozilla/4.0 (compatible; MSIE 6.0; Win 12.229.35.165 - - [12/Mar/2003:04:34:11 -0800] GET /e/t/applogic/spacer.gif HTTP/1.1 404 0 https://www.here.com/t 24.98.188.254 - - [12/Mar/2003:04:43:19 -0800] GET /e/t/kc/images/liquidation_rights.gif HTTP/1.1 404 0 https://www.h 207.5.195.98 - - [12/Mar/2003:04:56:32 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https://www.here.com/t I'm guessing I need the three digits that are located on each line between 'HTTP/\d.\d ' and another ' ' but not sure how to do that. You've almost done it yourself. I've never really worked with HTTP log data, but from the lines that you've given, I would say: my ($error_code); if ($ln =~ / HTTP\/\d\.\d\ (\d+)/) { ($error_code) = $1; } bye George P. Thanks much, Derek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
On Wed, 12 Mar 2003, Brett W. McCoy wrote: You're not capturing the correct string. Here's a code snippet I just tried on an Apache log that worked (assuming you have an open file handle): while(LOG) { print $1\n if m|HTTP.*\s+(\d{3})|g'; } $1 contains the matched string inside the parens (\d{3}). Dang it, when I cut and pasted, a rogue ' got into the code somehow. That final ' on the regular expression should not be there. -- Brett http://www.chapelperilous.net/ You are not a fool just because you have done something foolish -- only if the folly of it escapes you. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions http error code
Using your idea I ended up with data like this. Which is odd because the database should only include 400 and 500 type errors. 176 404 370 157 404 370 526 178 176 404 526 526 So I went ahead and modified it to print the code and the dataline and got this: And got this: 404 24.54.175.153 - - [11/Mar/2003:07:48:37 -0800] GET /e/t/invest/img/spacer.gif HTTP/1.1 404 0 https:// 370 209.91.198.57 - - [11/Mar/2003:07:48:24 -0800] GET /e/t/search/aaa?qmenu=2sym=dyn, intc HTTP/1.0 400 370 526 66.196.65.24 - - [11/Mar/2003:07:54:32 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/5.0 (Slur 178 167.127.163.141 - isklvjyy [11/Mar/2003:08:02:46 -0800] GET /e/t/aaa HTTP/1.1 500 178 - Mozilla/4.0 (compatible 404 68.39.167.38 - - [11/Mar/2003:08:06:34 -0800] GET /e/t/aaa/img/spacer.gif HTTP/1.1 404 0 https://us.etrade.com/e/ 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en 526 65.248.129.126 - - [11/Mar/2003:08:03:20 -0800] GET /mod_ssl:error:HTTP-request HTTP/1.0 400 526 - Mozilla/4.0 [en The 404's were right but the rest took the second group of numbers instead of the needed first. This is how my code looked: my $code,$msg; foreach (@RAW_DATA) { $code = $1 if m|HTTP.*\s+(\d{3})|g; ($timestamp, $msg) = split(/\t/); if (!$code) { print NEXT\n; next; } print $code\t$msg\n; $code = 0; } I did manage to get a version of George's to work. Still interested in trying all variations though. Derek -Original Message- From: Brett W. McCoy [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 12, 2003 11:02 AM To: Romeyn, Derek Cc: 'George P.'; '[EMAIL PROTECTED]' Subject: RE: Regular Expressions http error code On Wed, 12 Mar 2003, Brett W. McCoy wrote: You're not capturing the correct string. Here's a code snippet I just tried on an Apache log that worked (assuming you have an open file handle): while(LOG) { print $1\n if m|HTTP.*\s+(\d{3})|g'; } $1 contains the matched string inside the parens (\d{3}). Dang it, when I cut and pasted, a rogue ' got into the code somehow. That final ' on the regular expression should not be there. -- Brett http://www.chapelperilous.net/ You are not a fool just because you have done something foolish -- only if the folly of it escapes you. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions http error code
Romeyn, Derek wrote: K, I tried this and it didn't work as expected: $code =~ / HTTP\/\d\.\d\ (\d+)/; if (!$code) { print NEXT\n; next; } print $code\n; The loop just printed NEXT 300 or so times. I was thinking that $code would equal whatever was in the parentheses. Am I still not getting this? Probably not. The code did exactly what you told it to do. It printed the string 'NEXT, followed by a newline. Presumably you have this code inside a loop which runs 300 or so times, as there is nothing in the code posted that would make it loop. Is there some reason you expected the bare string NEXT to hold some value? Did you do anything to assign a value to it? As to the regular expression, try: perldoc perlre The parentheses capture values, which will remain until the next time a regex is used, in the form: $1, $2 ... $n where n is equal to. the number of matches found. In this context, $role is the string being tested. Always read the manual before trying to use a sophisticated tool. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regular expressions / clickable links in text
Hohokus == Hohokus Wombat [EMAIL PROTECTED] writes: Hohokus i want to take a chunk of text and convert anything that's a link to Hohokus something clickable in the browser. (this is being used in 'request Hohokus tracker', if anyone's interested.) Hohokus there's already a chunk of code that does this: There's a better chunk in the module URI::Find. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 [EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regular expressions
Evan N Mr Niso/Lockheed Martin Kehayias wrote: Greetings, Hello, I am attempting to limit entries a user could make when inputting names into one of my scripts. I prompt the user to enter one or more names. One name is easy to isolate but when there are more I want to support commas. At the same time I don't want to accept anything other than names and commas. I want to force the user to either enter names correctly or exit. For that matter it would be really cool if I could test the names against /etc/passwd. You can. $ perl -le' if ( defined( getpwnam( $ARGV[0] ) ) ) { print $ARGV[0] found; } else { print $ARGV[0] NOT found; } ' root root found $ perl -le' if ( defined( getpwnam( $ARGV[0] ) ) ) { print $ARGV[0] found; } else { print $ARGV[0] NOT found; } ' fred fred NOT found John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: regular expressions
At this point could a user still input something like 7mary3. Ideally a user can enter a name or several separated by commas but *nothing* else. Convention would be lowercase firstinitiallastname It seems like Rob's is really close but I don't understand all of the code. I am a little shy with arrays. Is @names built as you go? And, is the input all one element? It seems to me as if that is the case but not 100% sure. -Original Message- From: Rob Dixon [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 10:43 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hi Paul See below. Paul Kraus [EMAIL PROTECTED] wrote in message 021b01c2b7f3$490ed540$64fea8c0@pkrausxp">news:021b01c2b7f3$490ed540$64fea8c0@pkrausxp... Correct me if I am wrong but wouldn't @names = sprit /,/,$ans; That's pretty much what I did, except that I added optional leading and trailing whitespace to the regex so that these would be trimmed from the names. I didn't actually do the job properly though, because whitespace at the start and end of $ans won't be removed, and input like ' Henrietta' will fail. Nearly right though! Then you could perform your tests against the array elements. if (/[A-Za-z]+/) Not really. That only checks to see if the name contains at least one alpha. I did {/[^a-zA-Z]/ which checks for at least one non-alpha, when the loop continues and reports an error. Also you don't need the '+' modifier. Assuming that the names would only contain those characters. or if (/\w+/) meaning all word characters. /\W/ would do in my code, but its unwholesome inclusion of the underscore in a valid 'word' character makes it much less useful for non-programming apps. Do the same thing. Splitting everything separated by a comma? Unsure what you mean here :-? I'll pretend you didn't say it. Cheers, Rob -Original Message- From: Rob Dixon [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 10:06 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hello, erm, Evan N Mr Niso/Lockheed Martin Kehayias This should do what you want: my @names; do { errmesg() if @names; my $ans = STDIN; @names = split /\s*,\s*/, $ans; } while (grep {/[^a-zA-Z]/} @names); which splits on commas with any amount of preceding and trailing whitespace. Names have to be alphabetic. HTH, Rob Evan N Mr Niso/Lockheed Martin Kehayias [EMAIL PROTECTED] wrote in message 90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144">news:90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144... Greetings, I am attempting to limit entries a user could make when inputting names into one of my scripts. I prompt the user to enter one or more names. One name is easy to isolate but when there are more I want to support commas. At the same time I don't want to accept anything other than names and commas. I want to force the user to either enter names correctly or exit. For that matter it would be really cool if I could test the names against /etc/passwd. But beggars can't be choosers I will tackle the array piece later. What the user sees: Please enter user name(s): If more than one separate using commas example (single name): evan example (multi name): debbie, clint, henry So far what broken pieces I have... #!/usr/bin/perl while($ans !~ /^[a-z]+$/ || $ans !~ /^[a-z]+\,?[a-z]*$/) { errmesg (); $ans = STDIN; }; sub errmesg { print \nType user name(s) and press enter:\n; print note: if more than one separate using commas\n; print example (single name): evan\n; print example (multi name): debbie, clint, henry\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: regular expressions
What I posted earlier is pretty much it. I basically want to change the contents of a sudoers file for sudo. I make almost all of the other modifications in another script. In the current script I want to find the line that says User_AliasACLPUSHER in the sudoers file and replace it with itself and append = user1, user2, user3. Of course users 1 through 3 are relative to the operator input. The line I am searching for is: User_AliasACLPUSHER = test1, test2#where test1 and test2 may vary Okay I am pretty lame... I have a line in there using sed but I will change it before I am done. But basically I find the line User_Alias.*ACLPUSHER and replace it. #!/usr/bin/perl while($ans !~ /^[a-z]+$/ || $ans !~ /^[a-z]+\,?[a-z]*$/) { errmesg (); $ans = STDIN; }; `sed 's/^\(User_Alias.*ACLPUSHER\).*/\1 = '$NAME'/' s2`; sub errmesg { print \nType user name(s) and press enter:\n; print note: if more than one separate using commas\n; print example (single name): evan\n; print example (multi name): debbie, clint, henry\n; } Thanks again for your help, Evan P.S. Do you know if there is an ettiquette or rules page for this list? I have received mail from it for a long time but haven't posted much. -Original Message- From: Paul Kraus [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 11:07 AM To: Kehayias, Evan N Mr NISO/Lockheed Martin Subject: RE: regular expressions split takes an line and splits it by deliminator (perldoc -f split) so if your input was paul, david, kraus\n then $names[0]=paul $names[1]= david #notice the space. $names[2]= kraus\n #notice the new line. If you did not want the new line make sure you chomp your input. Why don't you post your script so far so we have a better idea of what your trying to do. -Original Message- From: Kehayias, Evan N Mr NISO/Lockheed Martin [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 11:00 AM To: [EMAIL PROTECTED] Cc: 'Rob Dixon'; 'Paul Kraus' Subject: RE: regular expressions At this point could a user still input something like 7mary3. Ideally a user can enter a name or several separated by commas but *nothing* else. Convention would be lowercase firstinitiallastname It seems like Rob's is really close but I don't understand all of the code. I am a little shy with arrays. Is @names built as you go? And, is the input all one element? It seems to me as if that is the case but not 100% sure. -Original Message- From: Rob Dixon [ mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ] Sent: Thursday, January 09, 2003 10:43 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hi Paul See below. Paul Kraus [EMAIL PROTECTED] wrote in message 021b01c2b7f3$490ed540$64fea8c0@pkrausxp">news:021b01c2b7f3$490ed540$64fea8c0@pkrausxp 021b01c2b7f3$490ed540$64fea8c0@pkrausxp">news:021b01c2b7f3$490ed540$64fea8c0@pkrausxp ... Correct me if I am wrong but wouldn't @names = sprit /,/,$ans; That's pretty much what I did, except that I added optional leading and trailing whitespace to the regex so that these would be trimmed from the names. I didn't actually do the job properly though, because whitespace at the start and end of $ans won't be removed, and input like ' Henrietta' will fail. Nearly right though! Then you could perform your tests against the array elements. if (/[A-Za-z]+/) Not really. That only checks to see if the name contains at least one alpha. I did {/[^a-zA-Z]/ which checks for at least one non-alpha, when the loop continues and reports an error. Also you don't need the '+' modifier. Assuming that the names would only contain those characters. or if (/\w+/) meaning all word characters. /\W/ would do in my code, but its unwholesome inclusion of the underscore in a valid 'word' character makes it much less useful for non-programming apps. Do the same thing. Splitting everything separated by a comma? Unsure what you mean here :-? I'll pretend you didn't say it. Cheers, Rob -Original Message- From: Rob Dixon [ mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ] Sent: Thursday, January 09, 2003 10:06 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hello, erm, Evan N Mr Niso/Lockheed Martin Kehayias This should do what you want: my @names; do { errmesg() if @names; my $ans = STDIN; @names = split /\s*,\s*/, $ans; } while (grep {/[^a-zA-Z]/} @names); which splits on commas with any amount of preceding and trailing whitespace. Names have to be alphabetic. HTH, Rob Evan N Mr Niso/Lockheed Martin Kehayias [EMAIL PROTECTED] wrote in message 90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144">news:90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144 90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144">news:90AFE0B84E52EE
RE: regular expressions
Correct me if I am wrong but wouldn't @names = sprit /,/,$ans; Then you could perform your tests against the array elements. if (/[A-Za-z]+/) Assuming that the names would only contain those characters. or if (/\w+/) meaning all word characters. Do the same thing. Splitting everything separated by a comma? -Original Message- From: Rob Dixon [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 10:06 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hello, erm, Evan N Mr Niso/Lockheed Martin Kehayias This should do what you want: my @names; do { errmesg() if @names; my $ans = STDIN; @names = split /\s*,\s*/, $ans; } while (grep {/[^a-zA-Z]/} @names); which splits on commas with any amount of preceding and trailing whitespace. Names have to be alphabetic. HTH, Rob Evan N Mr Niso/Lockheed Martin Kehayias [EMAIL PROTECTED] wrote in message 90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144">news:90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144... Greetings, I am attempting to limit entries a user could make when inputting names into one of my scripts. I prompt the user to enter one or more names. One name is easy to isolate but when there are more I want to support commas. At the same time I don't want to accept anything other than names and commas. I want to force the user to either enter names correctly or exit. For that matter it would be really cool if I could test the names against /etc/passwd. But beggars can't be choosers I will tackle the array piece later. What the user sees: Please enter user name(s): If more than one separate using commas example (single name): evan example (multi name): debbie, clint, henry So far what broken pieces I have... #!/usr/bin/perl while($ans !~ /^[a-z]+$/ || $ans !~ /^[a-z]+\,?[a-z]*$/) { errmesg (); $ans = STDIN; }; sub errmesg { print \nType user name(s) and press enter:\n; print note: if more than one separate using commas\n; print example (single name): evan\n; print example (multi name): debbie, clint, henry\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regular expressions
Hi Paul See below. Paul Kraus [EMAIL PROTECTED] wrote in message 021b01c2b7f3$490ed540$64fea8c0@pkrausxp">news:021b01c2b7f3$490ed540$64fea8c0@pkrausxp... Correct me if I am wrong but wouldn't @names = sprit /,/,$ans; That's pretty much what I did, except that I added optional leading and trailing whitespace to the regex so that these would be trimmed from the names. I didn't actually do the job properly though, because whitespace at the start and end of $ans won't be removed, and input like ' Henrietta' will fail. Nearly right though! Then you could perform your tests against the array elements. if (/[A-Za-z]+/) Not really. That only checks to see if the name contains at least one alpha. I did {/[^a-zA-Z]/ which checks for at least one non-alpha, when the loop continues and reports an error. Also you don't need the '+' modifier. Assuming that the names would only contain those characters. or if (/\w+/) meaning all word characters. /\W/ would do in my code, but its unwholesome inclusion of the underscore in a valid 'word' character makes it much less useful for non-programming apps. Do the same thing. Splitting everything separated by a comma? Unsure what you mean here :-? I'll pretend you didn't say it. Cheers, Rob -Original Message- From: Rob Dixon [mailto:[EMAIL PROTECTED]] Sent: Thursday, January 09, 2003 10:06 AM To: [EMAIL PROTECTED] Subject: Re: regular expressions Hello, erm, Evan N Mr Niso/Lockheed Martin Kehayias This should do what you want: my @names; do { errmesg() if @names; my $ans = STDIN; @names = split /\s*,\s*/, $ans; } while (grep {/[^a-zA-Z]/} @names); which splits on commas with any amount of preceding and trailing whitespace. Names have to be alphabetic. HTH, Rob Evan N Mr Niso/Lockheed Martin Kehayias [EMAIL PROTECTED] wrote in message 90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144">news:90AFE0B84E52EE46A8CD1DB0789C0A860ED043@DADC144... Greetings, I am attempting to limit entries a user could make when inputting names into one of my scripts. I prompt the user to enter one or more names. One name is easy to isolate but when there are more I want to support commas. At the same time I don't want to accept anything other than names and commas. I want to force the user to either enter names correctly or exit. For that matter it would be really cool if I could test the names against /etc/passwd. But beggars can't be choosers I will tackle the array piece later. What the user sees: Please enter user name(s): If more than one separate using commas example (single name): evan example (multi name): debbie, clint, henry So far what broken pieces I have... #!/usr/bin/perl while($ans !~ /^[a-z]+$/ || $ans !~ /^[a-z]+\,?[a-z]*$/) { errmesg (); $ans = STDIN; }; sub errmesg { print \nType user name(s) and press enter:\n; print note: if more than one separate using commas\n; print example (single name): evan\n; print example (multi name): debbie, clint, henry\n; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions: Grouping and backreferences...
eric-perl wrote at Tue, 10 Sep 2002 09:32:22 +0200: How can I capture all the words that contain 'at' in the string 'A fat cat sat on my hat.'? Any pointers? $sentence = 'A fat cat sat on my hat.' $sentence =~ m/(\wat)/; returns: $1 = 'fat' As TMTWTDI, here's a solution without a global matching: my @at_words = grep /at/, split /\W+/, $sentence; Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions: Grouping and backreferences...
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Tuesday, September 10, 2002 12:32 AM To: Beginners Perl Mailing List Subject: Regular Expressions: Grouping and backreferences... Hello, All: How can I capture all the words that contain 'at' in the string 'A fat cat sat on my hat.'? Any pointers? $sentence = 'A fat cat sat on my hat.' $sentence =~ m/(\wat)/; .returns: $1 = 'fat' -Response Message- your regex will only match the letter before at and the 'at', all words containing 'at' is the following, placing them into the array called @list: $sentence = 'A fat cat sat on that hat.'; @list = $sentence =~ m/(\w*at\w*)/g; foreach (@list) {print $_\n} -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular Expressions: Grouping and backreferences...
On Tue, 2002-09-10 at 03:32, [EMAIL PROTECTED] wrote: Hello, All: How can I capture all the words that contain 'at' in the string 'A fat cat sat on my hat.'? Any pointers? $sentence = 'A fat cat sat on my hat.' $sentence =~ m/(\wat)/; returns: $1 = 'fat' -- Eric P. Sunnyvale, CA -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] You were on the right track, but you need to do global matching and define contains better. Here is what I would do. snip href=perldoc perlop The /g modifier specifies global pattern match ing--that is, matching as many times as possible within the string. How it behaves depends on the context. In list context, it returns a list of the substrings matched by any capturing parenthe ses in the regular expression. If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern. /snip code #!/usr/bin/perl my $str = 'A fat cat sat on my hat and attacked me.'; my @at_words = $str =~ /(\w*at\w*\b)/g; print @at_words\n; /code output fat cat sat hat attacked /output -- Today is Pungenday the 34th day of Bureaucracy in the YOLD 3168 Or not. Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Regular expressions
on Fri, 19 Jul 2002 11:39:21 GMT, Matt Wetherill wrote: Now I know that the perl function localtime returns a date formatted like: Fri Jul 19 12:20:26 2002 So I'm nearly there - all I need to do is remove the 'day of the week' from the start of the string (e.g. Fri ) and the time from the end (e.g. 12:20:26 2002). I guess that a regular expression could do all this, but I'm struggling - any ideas would be greatly appreciated. You don't need a regular expression, you can use: my $d = join , (split /\s+/, localtime)[1,2]; -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: regular expressions in rindex function
You can remove the whitespace at the end of a line with this regex... $text =~ s/\s+$//; It matches one or more whitespace chars at the end of the line and replaces them with nothing. Rob -Original Message- From: Richard Pfeiffer [mailto:[EMAIL PROTECTED]] Sent: Monday, March 18, 2002 2:56 PM To: [EMAIL PROTECTED] Subject: regular expressions in rindex function Good Afternoon, Wondering if anyone could help me with this or enlighten me on another way to go about it. I have a string that has various white space (tabs, spaces, etc) at the end of it. I want to find the last real char [0-9a-zA-Z] and then use chop or chomp to remove the whitespace. I've tried: rindex($file, t); which of course works if the last char is a 't', but I certainly cannot always be assured of this. However, none of the following word char expressions are working: rindex($file, /[0-9a-zA-Z]/); rindex($file, /[0-9a-zA-Z]/); rindex($file, [0-9a-zA-Z]); rindex($file, /[\w]/); .so either I'm doing it wrong or I can't use a regular expression within rindex. (My vote goes for the former!) Thanks! -R -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: regular expressions in rindex function
On Mar 18, Richard Pfeiffer said: Good Afternoon, I have bad news for you. Your subject leads me to this statement: it doesn't work that way. I have a string that has various white space (tabs, spaces, etc) at the end of it. I want to find the last real char [0-9a-zA-Z] and then use chop or chomp to remove the whitespace. You're better off using something like $string =~ s/\s+$//; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[ADMIN REDIRECT] Re: Regular Expressions
Hello, I am redirecting this question from beginners-cgi to beginners. Please respond to the original poster ([EMAIL PROTECTED]) and the beginners list. Please remember that non-CGI related questions should not be send to beginners-cgi. Thanks. Cheers, Kevin On Wed, Feb 20, 2002 at 01:13:55PM -0300, Lilian Alvarenga Caravela Godoy ([EMAIL PROTECTED]) said something similar to: Hi everyone I am trying to learn Perl looking into some scripts. One of them has a regular expression inside. First of all, I need to know what two specific lines are doing. The code is bellow. $req =~ s/\r//g ; $req =~ s/([^\n]{72,72})\n([^\n]{1,71})\n([^\n]{1,71})$/$1\n$2$3/ I know they are replacing some things but cannot understand what. Specially the second one. I would really appreciate if somebody could explain to me what that regular expression means. It is kind of an emergency. And, if someone knows a link where there is more information about regular expressions, I would be grateful. Thanks in advance. Lilian -- [Writing CGI Applications with Perl - http://perlcgi-book.com] Everywhere is walking distance if you have the time. -- Steven Wright -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[ADMIN REDIRECT] Re: Regular Expressions
Hello, I am redirecting this question from beginners-cgi to beginners. Please respond to the original poster ([EMAIL PROTECTED]) and the beginners list. Please remember that non-CGI related questions should not be send to beginners-cgi. Thanks. Cheers, Kevin On Wed, Feb 20, 2002 at 01:13:55PM -0300, Lilian Alvarenga Caravela Godoy ([EMAIL PROTECTED]) said something similar to: Hi everyone I am trying to learn Perl looking into some scripts. One of them has a regular expression inside. First of all, I need to know what two specific lines are doing. The code is bellow. $req =~ s/\r//g ; $req =~ s/([^\n]{72,72})\n([^\n]{1,71})\n([^\n]{1,71})$/$1\n$2$3/ I know they are replacing some things but cannot understand what. Specially the second one. I would really appreciate if somebody could explain to me what that regular expression means. It is kind of an emergency. And, if someone knows a link where there is more information about regular expressions, I would be grateful. Thanks in advance. Lilian -- [Writing CGI Applications with Perl - http://perlcgi-book.com] Everywhere is walking distance if you have the time. -- Steven Wright -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions - matching the first time
On Feb 15, Russ Foster said: As of Perl 5.6.2 (not released yet), /^(.*?):/ and /^([^:]*):/ will have the same efficiency (read: speed). If you're curious, currently /^.*?:/ and /^[^:]*:/ have the same speed -- it's the capturing that killed .*?, but I have fixed that. Just to be clear, it's the capturing (.*?) of the string that suffers the most (performance-wise), correct? What do you mean you have it fixed--that future versions of Perl don't suffer a hit? It's not so much the ACTUAL capturing. Capturing /(.*?):/ and capturing /([^:]*):/ requires the same effort. What killed /(.*?):/ is that normally, Perl's regex engine would optimize /.*?:/ to make .*? jump from one colon to the next in the string. However, because of the internal representation of /(.*?):/, it couldn't do that optimization, and ..*? would move one character at a time, instead of the optimized jump. I made it look a little hard for that optimization. And personally, I'd use /([^:]*)/ instead of /^([^:]*):/, since they match the same thing (assuming there IS a colon in the string). Or I'd use /(.*?):/ instead of /^(.*?):/ but whatever. What's the reason for preferring not to use the '^' ? Is it speed? Or just a desire to make a regex a simple as possible? Personal style. Regexes are already crufty enough -- if I can reduce the amount of content in the regex itself, that's good for me. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions - matching the first time
As of Perl 5.6.2 (not released yet), /^(.*?):/ and /^([^:]*):/ will have the same efficiency (read: speed). If you're curious, currently /^.*?:/ and /^[^:]*:/ have the same speed -- it's the capturing that killed .*?, but I have fixed that. Just to be clear, it's the capturing (.*?) of the string that suffers the most (performance-wise), correct? What do you mean you have it fixed--that future versions of Perl don't suffer a hit? And personally, I'd use /([^:]*)/ instead of /^([^:]*):/, since they match the same thing (assuming there IS a colon in the string). Or I'd use /(.*?):/ instead of /^(.*?):/ but whatever. What's the reason for preferring not to use the '^' ? Is it speed? Or just a desire to make a regex a simple as possible? Personally, I prefer making my regexs (and scripts, in general) as explicit as possible, even if it means taking a performance hit. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions - matching the first time
The * operator is greedy, grabbing as much of the string as it can with still being able to match. The ? operator limits this behavior, so I think what you want is: $String =~ /^(.*?):/; /\/\ark -Original Message- From: Russ Foster [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 2:10 PM To: '[EMAIL PROTECTED]' Subject: Regular Expressions - matching the first time I have string, something like: $String = aaa bbb: fffd: sdfsdf qweqd: adfsdf qwcdsfs: qwdq qchuti: qwiojd; Now, I want to extract everything from the start of the string, up through the FIRST colon : -- in this case aaa bbb. My regex looks like this: $String =~ /^(.*):/ ; So: ^ starts at the beginning, (.*) to grab everything in the middle, and : to match the colon. What I am getting is, everything from the start of the string up until the last colon. In this example: $1 = aaa bbb: fffd: sdfsdf qweqd: adfsdf qwcdsfs: qwdq qchuti What I want is: $1 = aaa bbb I even tried using: $String =~ /^(.*):{1}/ ; Thinking the {1} would match the first occurance of :, but that didn't change the results (I was runningn out of ideas). Now, I could split $String on :, but I have to think that there is a better way and I'm missing some modifier that I can't find in the FAQ or any of the three books that have been dumped on my desk. Any help is appreciated. Russell J Foster Subject, Wills, Company e. [EMAIL PROTECTED] v. 630-572-0240 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions - matching the first time
all quantifiers (ie * + {2,7} ) are all greedy which means they will try to match as much as possible as long as it's true. To make the quantifier not greedy put a ? (question mark) after it, then it will match the least as possible as long as it's true. what you want is $String =~ /^(.*?):/; now $1 is everything up to the first : excluding the colon. -Original Message- From: Russ Foster [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 5:10 PM To: '[EMAIL PROTECTED]' Subject: Regular Expressions - matching the first time I have string, something like: $String = aaa bbb: fffd: sdfsdf qweqd: adfsdf qwcdsfs: qwdq qchuti: qwiojd; Now, I want to extract everything from the start of the string, up through the FIRST colon : -- in this case aaa bbb. My regex looks like this: $String =~ /^(.*):/ ; So: ^ starts at the beginning, (.*) to grab everything in the middle, and : to match the colon. What I am getting is, everything from the start of the string up until the last colon. In this example: $1 = aaa bbb: fffd: sdfsdf qweqd: adfsdf qwcdsfs: qwdq qchuti What I want is: $1 = aaa bbb I even tried using: $String =~ /^(.*):{1}/ ; Thinking the {1} would match the first occurance of :, but that didn't change the results (I was runningn out of ideas). Now, I could split $String on :, but I have to think that there is a better way and I'm missing some modifier that I can't find in the FAQ or any of the three books that have been dumped on my desk. Any help is appreciated. Russell J Foster Subject, Wills, Company e. [EMAIL PROTECTED] v. 630-572-0240 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]