Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

regex to match words and numbers

by doubledecker (Scribe)
on Jun 08, 2011 at 13:10 UTC ( [id://908705]=perlquestion: print w/replies, xml ) Need Help??

doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

help me out in providing the regex to match the data which satisfies the following criteria should be rejected

  1. Any line starting with *
  2. Any line starting with a word
  3. Any line starting with digit and .
#!/usr/bin/perl use strict; use warnings; my @chunks = <DATA>; # only 12345 is to be matched foreach my $i ( 0 .. @chunks-1 ) { my $data = $chunks[$i]; no warnings; if ( $data =~m/[^\d+\.?\*?]/ ) { print "Number Matched\n"; } } __DATA__ 1. For further assistance email us at 121@azing.com', * You can also highlight any matter related to our Officer', Page 2, 12345

Replies are listed 'Best First'.
Re: regex to match words and numbers
by moritz (Cardinal) on Jun 08, 2011 at 13:16 UTC
    How about
    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.

      next if $data =~ /^[\pL\pN.*]/; doesn't appear to accept anything of interest:

      >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'?

        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.

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.

      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 '.'

        I agree. I originally had the group-no-capture, then foolishly removed them.
Re: regex to match words and numbers
by zek152 (Pilgrim) on Jun 08, 2011 at 13:21 UTC

    Seems like you just need to look at the first 1-2 character(s).

    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
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; }
Re: regex to match words and numbers
by planetscape (Chancellor) on Jun 09, 2011 at 04:02 UTC
Re: regex to match words and numbers
by Anonymous Monk on Jun 08, 2011 at 14:13 UTC
    This smells like homework...

      ATP (aroma transfer protocol) isn't part of my TCP/IP stack yet so I can't comment as to the smell but I don't think it's homework. Rationale below:

      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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://908705]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found