Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Regular expression help

by ashnator (Sexton)
on Oct 23, 2008 at 03:56 UTC ( [id://718937]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
$string = "Required Not_Required Not_Required Required";
How can i use regular expression to get only required fields and then print using $& ?

Replies are listed 'Best First'.
Re: Regular expression help
by ChOas (Curate) on Oct 23, 2008 at 07:02 UTC
    You mean literally ?

    print "'$&'\n" while ($string=~/\bRequired\b/g);


    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: Regular expression help
by johngg (Canon) on Oct 23, 2008 at 10:15 UTC
    You can use capturing parentheses and a global match. Avoid using $&, $` and $' if you can as once used in a script they impose a performance penalty on all regular expressions in that script.

    #!/usr/bin/perl -l # use strict; use warnings; my @required = ( q{this}, q{that}, q{the other}, q{bat}, ); my $rxRequired = do { local $" = q{|}; qr{(?i)\b(@required)\b}; }; my $text = q{This afternoon I found a bat in the other bath, that was odd!}; print for $text =~ m{$rxRequired}g;

    The output.

    This bat the other that

    I hope this is useful.

    Cheers,

    JohnGG

Re: Regx help
by ikegami (Patriarch) on Oct 23, 2008 at 04:15 UTC
    print("$_\n") for ( split(/ /, $string) )[0,3];

    I have no idea what $& has to do with anything. Or regular expressions, really, but I did end up using one.

Re: Regular expression help
by brsaravan (Scribe) on Oct 23, 2008 at 05:29 UTC
    $& - Contains the string matched by the last pattern match.
    use strict; my $string = "Required Not_Required Not_Required Required"; my @words = split(" ",$string); map {$_ =~ /^Required$/g;print "$&"}@words;
      Some questions regarding your solution.
      1. Why the /g modifier considering that you're matching (in scalar context) against $_ only once before $_ changes?
      2. Why a regexp anyway? Considering that "Required" is a fixed string, and you anchor it on both ends, $_ eq "Required" will do as well.
      3. Why quotes around $&?
Re: Regular expression help
by Anonymous Monk on Oct 23, 2008 at 06:12 UTC
    How can i use regular expression to get only required fields and then print using $& ?
    Why are you focusing on $&?
Re: Regular expression help
by JavaFan (Canon) on Oct 23, 2008 at 09:56 UTC
    say $& while $string =~ /\bRequired\b/g;
Re: Regular expression help
by moritz (Cardinal) on Oct 23, 2008 at 07:13 UTC
    my $result = join ' ', (split m/ /, $string)[0, 3];
Re: Regular expression help
by Wiggins (Hermit) on Oct 23, 2008 at 17:24 UTC
    I think there is a communications fault here. Does the phrase "only required fields" in the question mean fields whose content is the string "required" (which would be none); or is the question about how to extract only the first and last, space separated tokens from a string.

    Is the $string in the question literal, or simply showing the position of "required fields"?

    Clarification please....

Re: Regx help
by Punitha (Priest) on Oct 23, 2008 at 04:22 UTC

    Hi

    Please take a note about the regular expressions here http://perldoc.perl.org/perlretut.html and do you want something like this?

    use strict; my $string = "Required Not_Required Not_Required Required"; while($string=~/\bRequired\b/gi){ print "$&\n";##Here it will print only required text with non word + character before and after }

    Punitha

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://718937]
Approved by ikegami
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-04-20 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found