RE: Trouble with m///g
I think this might work. /\b\d{4}\b/ Rob -Original Message- From: Chap Harrison [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 10:38 AM To: [EMAIL PROTECTED] Subject: Trouble with m///g Hi, I'm trying to extract all four-digit numbers from a string in one fell swoop, but I can't seem to come up with the proper regexp. This is my first time using /g in a match so maybe there's a trick I'm missing. For example, the string aa 444 - should yield , , , , , . Here's one attempt that I thought had a reasonable chance. - - - - - #!/usr/bin/perl -w my $foo = aa 444 -; my @a = ($foo =~ m'[\D^](\d{4})[\D$]'g); print $foo\n; print(join(:,@a).\n); - - - - - aa 444 - :: Thanks for your consideration, Chap -- 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: Trouble with m///g
For example, the string aa 444 - should yield , , , , , . That's actually kind of tricky. How about: $aa = aa 444 -; @aa = $aa =~ /(?!\d)\d{4}(?!\d)/g; print $_\n for @aa; That gets and also, which the \b solution skips. What it says is to get all groups of 4 numbers not following or followed by another number. Dave ps - also see perldoc -f perlre and look for zero-width negative look(ahead|behind) assertions -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Trouble with m///g
Please bottom post... I think this might work. It might, but doesn't. Some testing would be good before posting inaccurate responses. /\b\d{4}\b/ \b is matching on boundaries, so you miss the first set, and the set with the 'aa' around them, and then there is the set with the '-' Rob -Original Message- From: Chap Harrison [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 10:38 AM To: [EMAIL PROTECTED] Subject: Trouble with m///g Hi, I'm trying to extract all four-digit numbers from a string in one fell swoop, but I can't seem to come up with the proper regexp. This is my first time using /g in a match so maybe there's a trick I'm missing. For example, the string aa 444 - should yield , , , , , . Here's one attempt that I thought had a reasonable chance. - - - - - #!/usr/bin/perl -w my $foo = aa 444 -; my @a = ($foo =~ m'[\D^](\d{4})[\D$]'g); print $foo\n; print(join(:,@a).\n); - - - - - aa 444 - :: Thanks for your consideration, Chap Out of curiousity based on your description shouldn't it return, ::::::: Or do you really mean, you are trying to capture all 4 digit strings that are not in a string of longer digits? You need to be very explicit about what you are after. I think (and have tested) that, my @a = ($foo =~ m'(?!\d{4})\d{4}(?!\d)'g); Gives you want you want, though I don't claim to be a regex expert like others on the list (are experts, rather than claiming). And I *believe* says, match any 4 digit string not preceded by a 4 digit string and not followed by a digit. Works? 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: Trouble with m///g
Hmmm... m'\b(\d{4})\b'g aa 444 - ::: Doesn't give me or . I think the problem has to do with where m///g starts on subsequent iterations. The pattern specifies a delimiter for both the start and the end of the target substring, but that means it will want to find an ending delim on iteration n, followed by a beginning delim on iteration n+1. On Sep 30, 2004, at 9:41 AM, Hanson, Rob wrote: I think this might work. /\b\d{4}\b/ Rob -Original Message- From: Chap Harrison [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 10:38 AM To: [EMAIL PROTECTED] Subject: Trouble with m///g Hi, I'm trying to extract all four-digit numbers from a string in one fell swoop, but I can't seem to come up with the proper regexp. This is my first time using /g in a match so maybe there's a trick I'm missing. For example, the string aa 444 - should yield , , , , , . Here's one attempt that I thought had a reasonable chance. - - - - - #!/usr/bin/perl -w my $foo = aa 444 -; my @a = ($foo =~ m'[\D^](\d{4})[\D$]'g); print $foo\n; print(join(:,@a).\n); - - - - - aa 444 - :: Thanks for your consideration, Chap -- 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Trouble with m///g
Chap Harrison wrote: Hi, I'm trying to extract all four-digit numbers from a string in one fell swoop, but I can't seem to come up with the proper regexp. This is my first time using /g in a match so maybe there's a trick I'm missing. For example, the string aa 444 - should yield , , , , , . TIMTOWTDI: @list = grep length==4, /\d+/g -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with m///g
Chap Harrison wrote: I'm trying to extract all four-digit numbers from a string in one fell swoop, but I can't seem to come up with the proper regexp. This is my first time using /g in a match so maybe there's a trick I'm missing. For example, the string aa 444 - should yield , , , , , . Here's one attempt that I thought had a reasonable chance. - - - - - #!/usr/bin/perl -w my $foo = aa 444 -; my @a = ($foo =~ m'[\D^](\d{4})[\D$]'g); The first character class requires that the number is preceeded by a non-digit character. (The ^ character has no special meaning in a character class.) Since the first number is not preceeded by anything, is not matched. I suppose you meant to do: my @a = ($foo =~ m'(?:\D|^)(\d{4})(?:\D|$)'g); which gives ::: but that's not what you want either. The reason why e.g. is not matched is that the space after is included in the first match, so the second attempt to match starts at the first '2'... You'd better use extended patterns, i.e. zero-width assertions: my @a = $foo =~ /(?!\d)\d{4}(?!\d)/g; Read about extended patterns in perldoc perlre. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with m///g
On Sep 30, 2004, at 9:55 AM, Wiggins d Anconia wrote: Out of curiousity based on your description shouldn't it return, ::::::: Or do you really mean, you are trying to capture all 4 digit strings that are not in a string of longer digits? You need to be very explicit about what you are after. The example was intended to resolve the ambiguities of my informal description :-) You correctly surmised what I was after. my @a = ($foo =~ m'(?!\d{4})\d{4}(?!\d)'g); And your solution works. Now I'm going to study up on *how* it works! Thanks, and also thanks to Dave and Gunnar for what appears to be the same solution, and the references to extended patterns and zero-width assertions. Chap -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with m///g
TIMTOWTDI: @list = grep length==4, /\d+/g Shouldn't that be: @list = grep length==4, $foo =~ /\d+/g; Cool solution, I wouldn't have thought to do it that way. I'm getting varying Benchmarking results, though. I think it might have something to do with grep speedups from 5.6.1 to 5.8.0... can anyone confirm this? On a box with 4 Xeon 2gigs with 5.6.1 and Benchmark v1: Rate grep wregex regex grep 55586/s -- -13% -23% wregex 64061/s15% -- -12% regex 72569/s31%13% -- But, on another box with 1 AMD 1gig with 5.8.0 and Benchmark v1.0501: Rate wregex regex grep wregex 31437/s -- -14% -18% regex 36470/s16% ---5% grep 38212/s22% 5% -- Confusing! #!/usr/bin/perl -w use strict; use Benchmark qw/cmpthese/; my ($aa); $aa = aa 444 -; sub regex { my @aa = $aa =~ /(?!\d)\d{4}(?!\d)/g } # Wiggins ;-) sub wregex { my @aa = $aa =~ /(?!\d{4})\d{4}(?!\d{4})/g } sub grep { my @aa = grep length==4, $aa =~ /\d+/g } cmpthese(10, { regex = \regex, wregex = \wregex, grep = \grep, }); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with m///g
Chap Harrison wrote on 30.09.2004: On Sep 30, 2004, at 9:55 AM, Wiggins d Anconia wrote: Out of curiousity based on your description shouldn't it return, ::::::: Or do you really mean, you are trying to capture all 4 digit strings that are not in a string of longer digits? You need to be very explicit about what you are after. The example was intended to resolve the ambiguities of my informal description :-) You correctly surmised what I was after. my @a = ($foo =~ m'(?!\d{4})\d{4}(?!\d)'g); Careful, you mistyped the original proposition: my @a = ($foo =~ m'(?!\d)\d{4}(?!\d)'g); This one will find a string consisting of four digits, neither preceded nor followed by a digit. In other words: exactly four digits. Your quote will find a string of four digits not preceded by another four digits, so it could find a string of five, six or seven digits. - Jan -- How many Microsoft engineers does it take to screw in a lightbulb? None. They just redefine dark as the new standard. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Trouble with m///g
On Sep 30, 2004, at 10:41 AM, Jan Eden wrote: my @a = ($foo =~ m'(?!\d{4})\d{4}(?!\d)'g); Careful, you mistyped the original proposition: my @a = ($foo =~ m'(?!\d)\d{4}(?!\d)'g); Oops, sorry - I copied that into the email from Wiggins' reply, but actually tested with Dave Gray's. Didn't notice the difference. What you posted gives the solution I was after. Thanks for the scrutiny! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response