Re: regex to match words and numbers
by moritz (Cardinal) on Jun 08, 2011 at 13:16 UTC
|
while (my $data = <DATA>) {
next if $data =~ /^[\pL\pN.*]/;
# do something with non-rejected $data here
}
\pL matches a letter, \pN a digit ("number"). Note that \w wouldn't work here, because it also matches the underscore.
| [reply] [Watch: Dir/Any] [d/l] |
|
>perl -wMstrict -le
"my @data = (
'1. for future assistance toss', '* You can also toss',
'Page 2, toss', '12345 keep this', '... keep this',
);
;;
for my $datum (@data) {
print qq{how about '$datum'?};
next if $datum =~ /^[\pL\pN.*]/;
print qq{keep it};
}
"
how about '1. for future assistance toss'?
how about '* You can also toss'?
how about 'Page 2, toss'?
how about '12345 keep this'?
how about '... keep this'?
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
The way I understood the OP, the regex should reject all of the example lines, because all of them start with one of (dot, star, word, digit).
That's what my regex does, and everything else (lines starting with a bang, slash, backslash, ...) is accepted.
If that's not what doubledecker wants, he or she should clarify the original posting.
| [reply] [Watch: Dir/Any] |
|
|
Re: regex to match words and numbers
by toolic (Bishop) on Jun 08, 2011 at 13:23 UTC
|
use warnings;
use strict;
my @chunks = <DATA>;
for (@chunks) {
print unless /^(?:[*]|[a-zA-Z]|\d+\.)/;
}
__DATA__
1. For further assistance email us at 121@azing.com',
* You can also highlight any matter related to our Officer',
Page 2,
12345
prints:
12345
Courtesy of YAPE::Regex::Exlpain:
Also, here is an explanation of your regex, which might help shed some light on why it doesn't work as you expect:
Update due to Animator's observation. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
Note that the regex (/^[*]|[a-zA-Z]|\d+\./)does not match what the OP wants.
The regex should contain a group or the '^' needs to be repeated.
That is: /^(?:[*]|[a-zA-Z]|\d+\.)/ OR /^[*]|^[a-zA-Z]|^\d+\./
(The regex as posted will return true when it contains a letter somewhere in the string)
Update: an example was requested but that request was later removed.
Anyway: an example as requesetd:
#!/usr/bin/perl
use warnings;
use strict;
my @chunks = <DATA>;
for (@chunks) {
print unless /^[*]|[a-zA-Z]|\d+\./;
}
__DATA__
a
@
b
@ d
e
Output:
@
The lines 'a', 'b', 'e' are rejected. (ok) The line '@' is not rejected. (ok) The line '@ d' is rejected. (not ok)
As far as I can tell '@ d' does not start with a '*'. It also does not start with a word and it also does not start with a number followed by a '.' | [reply] [Watch: Dir/Any] [d/l] [select] |
|
I agree. I originally had the group-no-capture, then foolishly removed them.
| [reply] [Watch: Dir/Any] |
Re: regex to match words and numbers
by zek152 (Pilgrim) on Jun 08, 2011 at 13:21 UTC
|
my @string = ('1. for future assistance ...','* You can also ...','Pag
+e 2,','12345');
foreach my $string (@string){
if(($string !~ /(^[0-9]\.)|(^[*])|(^[A-Za-z])/)){
print "$string\n";
}
}
#OUTPUT
#12345
| [reply] [Watch: Dir/Any] [d/l] |
Re: regex to match words and numbers
by happy.barney (Friar) on Jun 08, 2011 at 13:33 UTC
|
foreach my $i ( @chunks ) {
next if $i !~ m/ ^
(?!\*)
(?!(?!\d)\w)
(?!\d+\.)
/x;
print $i;
}
| [reply] [Watch: Dir/Any] [d/l] |
Re: regex to match words and numbers
by planetscape (Chancellor) on Jun 09, 2011 at 04:02 UTC
|
| [reply] [Watch: Dir/Any] |
Re: regex to match words and numbers
by Anonymous Monk on Jun 08, 2011 at 14:13 UTC
|
This smells like homework... | [reply] [Watch: Dir/Any] |
|
1. His questions have been more "real work" related.
2. It's the summer.
3. Few schools teach Perl.
However until I get ATP this is all speculation :) | [reply] [Watch: Dir/Any] [d/l] |