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

Pattern for a single word

by Nansh (Acolyte)
on Jun 18, 2017 at 11:42 UTC ( [id://1193049]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Suppose if i have a word like this

Eg: He_is_super_hero_Spiderman

He_was_a_super_actor_Tom

From above example i need to get only Spiderman and Tom to match that through pattern matching please help me to do so

Thanks

Replies are listed 'Best First'.
Re: Pattern for a single word
by LanX (Saint) on Jun 18, 2017 at 12:45 UTC
    Unclear!

    Why is Superman a "single word" but hero not?

    A precondition to clear programing is to ask clear questions.

    Don't keep us guessing.

    update

    to expand on Re: Pattern for a single word, showing us what you tried would clarify a lot.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Pattern for a single word
by choroba (Cardinal) on Jun 18, 2017 at 13:50 UTC
    Why do you want to select Spiderman or Tom?

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @strings = qw( He_is_super_hero_Spiderman He_was_a_super_actor_Tom ); for my $string (@strings) { my ($spiderman_or_tom) = $string =~ /Spiderman|Tom/g; say $spiderman_or_tom; my ($last_word) = (split /_/, $string)[-1]; say $last_word; my ($last_capitalized) = $string =~ /.*([[:upper:]][^_]+)/; say $last_capitalized; }

    Update: Explanation of the second solution: negative indices count from the right.
    Explanation of the third solution: .* matches as much as it can, then it captures the word starting with an uppercase letter and continuing with anything but underscore.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Pattern for a single word
by kcott (Archbishop) on Jun 19, 2017 at 05:55 UTC

    G'day Nansh,

    You don't need to fire up the regex engine for that. Take a look at substr and rindex: you'll also find they're a lot faster.

    $ perl -E 'say substr $_, 1 + rindex $_, "_" for qw{He_is_super_hero_S +piderman He_was_a_super_actor_Tom}' Spiderman Tom

    — Ken

Re: Pattern for a single word
by dbander (Scribe) on Jun 18, 2017 at 14:19 UTC

    It's difficult to understand what you're looking for. Three guesses:

    #!/usr/bin/perl use strict; use warnings; my @wordlist = ( 'He_is_super_hero_Spiderman', 'He_was_a_super_actor_Tom', 'This_line_is_not_supposed_to_match', ); # Showing the original list print "Original list:\n"; foreach my $word_element (@wordlist) { print "$word_element\n"; } print "\n"; # Returning whole line of any containing matching text print "Finding whole lines containing matches:\n"; foreach my $word_element (@wordlist) { if ($word_element =~ /Spiderman|Tom/) { print "$word_element\n"; } } print "\n"; # Using $1 to extract the found text print "Finding and extracting matches:\n"; foreach my $word_element (@wordlist) { if ($word_element =~ /(Spiderman|Tom)/) { print "$1\n"; } } print "\n"; # Using split to break up the words print "Not actually looking for matches, just extracting the last word +:\n"; foreach my $word_element (@wordlist) { my @wordlettes = split /\_/, $word_element; print "$wordlettes[-1]\n"; } print "\n"; exit;

    Results:

    Original list: He_is_super_hero_Spiderman He_was_a_super_actor_Tom This_line_is_not_supposed_to_match Finding whole lines containing matches: He_is_super_hero_Spiderman He_was_a_super_actor_Tom Finding and extracting matches: Spiderman Tom Not actually looking for matches, just extracting the last word: Spiderman Tom match

Re: Pattern for a single word
by 1nickt (Canon) on Jun 18, 2017 at 13:47 UTC

    Try playing with this:

    use strict; use warnings; use feature 'say'; for my $string (<DATA>) { chomp $string; if ( $string =~ / (?<!_) _ ([a-zA-Z]+) $ /x ) { say "Found $1 in $string"; } else { say "No match in $string"; } } __DATA__ He_is_super_hero_Spiderman He_was_a_super_actor_Tom He_was_a_ He_was_a_42 That_nansh_he_was_a_right__one

    Besides reading perlretut as suggested by the Anonymous One, you should consider what you are really trying to do here. If your strings really look like this, it indicates that you are doing some awkward text munging with split and join to get to this point. There is almost certainly a better way to do it. You may be suffering from the XY Problem syndrome. You should state what your overall objective is, instead of throwing out these snippets one at a time.


    The way forward always starts with a minimal test.
Re: Pattern for a single word
by Anonymous Monk on Jun 18, 2017 at 12:03 UTC
Re: Pattern for a single word
by sundialsvc4 (Abbot) on Jun 19, 2017 at 22:35 UTC

    Unfortunately, no one has yet invented a computer which can “guess” what you mean.   But nevertheless, regular-expressions do give you a lot options without obliging you to write any source-code.   For example, one possible variation would be this:

    /he.*super.*\_([a-z]+)$/i

    This particular regular-expression, for instance, looks for "he" followed by "super", so-far not caring where in the string these substrings occur nor what (if anything) surrounds them, so long as they do occur in that sequence.   But then, it does start to care:   it next wants to find an underscore followed by “one or more letters,” occurring at the end of the string.   The parentheses indicate that it wants to extract that group of letters, so that you can later retrieve what it has extracted for you.   Furthermore, the /i modifier tells us that the regex does not care about upper vs. lower-case characters, throughout.

    Is this the “right answer” for your problem?   Only y-o-u can know!

    However, if you will re-state your actual requirement more clearly and more fully, plenty of folks around here are happy to continue to help you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-05-27 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found