Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid
 
PerlMonks  

What is being matched?

by eversuhoshin (Sexton)
on Apr 02, 2011 at 02:47 UTC ( #897046=perlquestion: print w/ replies, xml ) Need Help??
eversuhoshin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks~

Could someone help me write a code that shows what part of the regular expression the text matched?

I have to match on bunch of words (about 15) to find the SEC filing that I want for instance "director" and "resignation."

Since I am still learning perl I was thinking of just creating separate variables indicating matched words

if $text=~m/\bdirector\b/ $director=1; if $text=~m/\bresignation\b/ $resign=1;

Is there a way I can do it more elegantly using

if $text=~m/\b(director|resignation)\b/ # code that shows which word matched;

Thank you again :)

Comment on What is being matched?
Select or Download Code
Re: What is being matched?
by ww (Bishop) on Apr 02, 2011 at 02:59 UTC
    Read perlretut with special attention to captures.
Re: What is being matched?
by graff (Chancellor) on Apr 02, 2011 at 04:47 UTC
    if ( $text=~m/\b(director|resignation)\b/ ) # code that shows which word matched: { if( $1 eq 'director') { print "That's the director? Get rid of him!\n"; } else { print "You can't resign, you're fired!\n"; } }

      Hello graff, I really like your print response hahaha :)

Re: What is being matched?
by AnomalousMonk (Monsignor) on Apr 02, 2011 at 09:23 UTC

    In 5.10+, see also named capture buffers:

    >perl -wMstrict -le "my $str = 'The CEO of WidgeCo resigned today.'; ;; $str =~ m{ (?<BIGWIG> chairman | president | ceo ) }xmsi; ;; if (exists $+{BIGWIG}) { print qq{a $+{BIGWIG} was mentioned}; } " a CEO was mentioned

    Update: For the position in the string of the match(es), see @- and @+ in perlvar.

Re: What is being matched?
by bart (Canon) on Apr 02, 2011 at 10:17 UTC
    my %count; while(/\b(director|resignation)\b/ig) { $count{$1}++; } my @found = sort keys %count; print "Found the words: @found\n";
Re: What is being matched?
by Marshall (Prior) on Apr 02, 2011 at 17:32 UTC
    If you are looking for a list of words, then you can use "match global" to find out which of those words occurred in the text. The result of this can be put into a hash table, like I did below using a simple map{} or assigned directly to an array, @matched.

    Then use logic to do more complicated AND's and OR's on the subset of the words that matched.

    #!/usr/bin/perl -w use strict; my $text = "Mr. X is a bozo and was hired and then resigned."; #build an "OR" regex from a list of search words #add \b (boundary) in front and back, then | between my @search_words = qw(bozo hired resigned); @search_words = map{"\\b$_\\b";}@search_words; my $search_regex = join("|",@search_words); #print "search_regex is: $search_regex\n"; #debug my %matched = map{$_=>1}($text =~ m/$search_regex/gi); #match global print "This bozo resigned - thank goodness!" if ($matched{bozo} and $matched{resigned}); #prints: This bozo resigned - thank goodness!
    Now having said that, perhaps we are answering the wrong question here? The SEC has the EDGAR (Electronic Data Gathering, Analysis, and Retrieval system) online. If you look here, main SEC filing forms site and look at EDGAR Searches, there is a button to do advanced boolean searches.The 10-K filings etc are in EDGAR, but I think there is some stuff that is missing - you would have to look. Perhaps, what you need is an LWP program that uses the EDGAR website? I don't know - just an idea.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-04-20 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls