Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

index regex

by welle (Beadle)
on Jun 08, 2013 at 10:00 UTC ( #1037823=perlquestion: print w/ replies, xml ) Need Help??
welle has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks

I have this simple Index line

my $result = index($line, " ");

what if I want the same $result not only if a space is found but also if other characters are found (as alternatives), let's say: [\(\)\,\.\;\:\-\?\!\'\] Is this possible?

Comment on index regex
Select or Download Code
Re: index regex
by LanX (Canon) on Jun 08, 2013 at 10:04 UTC
    plz use < code > tags

    see pos to find out the position of the last match.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Reaped: Re: index regex (dupe)
by NodeReaper (Curate) on Jun 08, 2013 at 10:04 UTC
Re: index regex
by CountZero (Bishop) on Jun 08, 2013 at 10:33 UTC
    use Modern::Perl; my $string = 'Search, find and tell me! where, oh where are those funn +y characters???'; my @positions; push @positions, [$1, pos($string)] while ($string =~ m/([-\s,.!?;])/g +); }
    @positions is an AoA: each element contains the character found and its position.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: index regex
by johngg (Abbot) on Jun 08, 2013 at 10:39 UTC

    Rather than index, which will not accept a pattern, use a character class in a capturing regex, match globally in a while loop and use the @- array, see perlvar. Note that a hyphen (-) in a character class has to be the first or last character or it will be interpreted as a range constructor, e.g. [a-z].

    $ perl -E ' > $line = > q{The line, as specified, has: no digits (0 to 9); letters!}; > say qq{>$1< found at $-[ 0 ]} while > $line =~ m{([ (),.;:?!-])}g;' > < found at 3 >,< found at 8 > < found at 9 > < found at 12 >,< found at 22 > < found at 23 >:< found at 27 > < found at 28 > < found at 31 > < found at 38 >(< found at 39 > < found at 41 > < found at 44 >)< found at 46 >;< found at 47 > < found at 48 >!< found at 56 $

    I hope this is helpful.

    Cheers,

    JohnGG

      If 5.10+ is available, use of the  //p regex modifier allows  ${^MATCH} (and friends) to be used eliminating the need for an explicit capture and perhaps slightly speeding the matching:

      >perl -wMstrict -lE "my $line = q{as specified, line has: no digits (0-9); letters!}; ;; say qq{'${^MATCH}' found at $-[0]} while $line =~ m{ [ (),.;:?!-] }xmsgp; " ' ' found at 2 ',' found at 12 ' ' found at 13 ' ' found at 18 ':' found at 22 ' ' found at 23 ' ' found at 26 ' ' found at 33 '(' found at 34 '-' found at 36 ')' found at 38 ';' found at 39 ' ' found at 40 '!' found at 48

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-07-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls