in reply to Re: perl system command
in thread perl system command

So far in order to get what I want I have to split it and then search through it variable $row is "We_need_feed"
my $row = $feed_db->fetchrow_array() ; my @words = split (/_/, $row); foreach(@words) { if(/feed*/) { $test="$_"; } } print("$test\n");

Replies are listed 'Best First'.
Re^3: perl system command
by AnomalousMonk (Archbishop) on Nov 10, 2016 at 03:21 UTC

    Might something like this be helpful?

    c:\@Work\Perl\monks>perl -wMstrict -le "my $look_for = qr{ (?<! [[:alpha:]]) home (?! [[:alpha:]]) }xms; ;; my $row = 'social_going_home'; my $match = my ($capture) = $row =~ m{ (?<= _) $look_for \z }xmsg; ;; if ($match) { print qq{match, captured '$capture'}; } " match, captured 'home'

    Update: BTW: The pattern  feed* in the match of  if(/feed*/) { ... } matches any of  fee feed feedd feeddd ... Is that what you wanted?


    Give a man a fish:  <%-{-{-{-<

      thank you , I will look at your code .Yes, I wanted feed and any words or digits after, because there is "feed001, feed002" etc
        ... I wanted feed and any words or digits after ...

        /d*/ matches zero or more  'd' characters, but does not match "words" (unless they're composed only of  'd' characters) and certainly not decimal digits. Perhaps you want something like  /feed[[:alnum:]]*/

        c:\@Work\Perl\monks>perl -wMstrict -le "for my $str (qw( feed feeder feed001 xfeed001x fee fee001 xfeex --fee-- fe xxx 001 )) { print qq{'$str' -> }, $str =~ m{ feed [[:alnum:]]* }xms ? '' : 'NO', ' match'; } " 'feed' -> match 'feeder' -> match 'feed001' -> match 'xfeed001x' -> match 'fee' -> NO match 'fee001' -> NO match 'xfeex' -> NO match '--fee--' -> NO match 'fe' -> NO match 'xxx' -> NO match '001' -> NO match
        Change the match pattern above to just  feed* to see the difference.


        Give a man a fish:  <%-{-{-{-<

Re^3: perl system command
by hippo (Archbishop) on Nov 10, 2016 at 09:31 UTC
    in order to get what I want

    It isn't overly clear what you want. If you have a string with words separated by underscores and just want to return the last such word then you could do this:

    my $row = 'We_need_feed'; my ($last) = $row =~ /[^_]*$/g; print $last;

    or you could split and then pop but maybe extracting the last isn't what you want at all.

      thank you , I learned from your example. I wanted to extract just the word "feed", I was using my $result=$row=~/feed*/m but $result would just contain 0 or 1 which I read is expected, but your way gets the phrase.Thank you , but what does /^_*$/ do? You are saying you are searching for underscore at the front, followed by anything at the end? and what does the brackets [] do? Thank you

        The square brackets indicate a character class and the caret inside negates what follows such that [^_] means any character other than an underscore. The dollar anchors it to the end. All this is in perlre and related pages which are well worth a read.