Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Find part of string

by Anonymous Monk
on Dec 11, 2006 at 15:53 UTC ( #589061=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Find part of string
by Jasper (Chaplain) on Dec 11, 2006 at 17:10 UTC
    my $matcher = /w/w_/w(?)_(1);

    I'd be very interested in listening to an explanation of how you think that might match _theWord. I'd be interested in that a great deal. No offence, but a tutorial into the very basics of constructing a regular expression would be very useful to you. Spoon feeding you a solution, like some other posters have done isn't going to help you learn. The regular expression matching _theWord should surely contain "_theWord" somewhere...?
Re: Find part of string
by madbombX (Hermit) on Dec 11, 2006 at 16:02 UTC
    I hate to be a stickler for details, but are you actually looking to find the text, /_theWord/ or are you looking to find text embedded in that format. If you are looking the /theWord/, then this should do you just fine (notice the ~ in the =~ which you left out in your code):
    my @matched_lines; push (@$matched_lines, $matcher) if ($matcher =~ /^.*_theWord$/);

    Also, but using an array, you can catch multiple matches in the same line. If you are looking for another grouping of text, then just reformat the regex for what you are looking for. Either that, or provide us a little more information so we can help you.

    Update: Upon another glance, I noticed that you are using multiple '/' in your regex. The first '/' begins the regex and the second '/' terminates it. Therefore, I believe your intended regex was something along the lines of $matcher =~ /\w\w_\w(.+?)/; However, this would still not match your data as the \w doesn't work the way you are thinking it does. I would recommend that you read perlretut or at least perlrequick to get a better understanding of regular expressions.

Re: Find part of string
by nimdokk (Vicar) on Dec 11, 2006 at 15:59 UTC
    Well, one way to do it might be as follows:

    while (<DATA>) { chomp; if (/_theWord/) { print "$_ contains '_theWord'\n"; } } __DATA__ ac_dajhta_theWord sd_dfdfe_fdfdv ca_hereeds_theWord se_dcvdfdef_dsfd cx_hserj_theWord fa_dferefvdvv_fadfde

    However, I'm sure others might come up with better regex's to use.

Re: Find part of string
by johngg (Canon) on Dec 11, 2006 at 16:48 UTC
    Assuming that you have the strings in an array and want the strings that match "_theWord" to be placed in another array, this should work.

    use strict; use warnings; my @strings = qw{ ac_dajhta_theWord sd_dfdfe_fdfdv ca_hereeds_theWord se_dcvdfdef_dsfd cx_hserj_theWord fa_dferefvdvv_fadfde}; my @matchingStrings = grep m{_theWord}, @strings; print qq{$_\n} for @matchingStrings;

    The output is

    ac_dajhta_theWord ca_hereeds_theWord cx_hserj_theWord

    I hope this is of use.



    Update: corrected wonky <code> tag

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-04-11 05:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found