Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to find substring using regex

by harshmane (Initiate)
on Jun 22, 2011 at 17:13 UTC ( #910943=perlquestion: print w/replies, xml ) Need Help??

harshmane 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: how to find substring using regex
by toolic (Bishop) on Jun 22, 2011 at 17:31 UTC
    Tips:
    • Use code tags: Writeup Formatting Tips
    • Post code that does not have syntax errors (the code you actually ran).
    • Tell us what you expect the count to be.
Re: how to find substring using regex
by ww (Archbishop) on Jun 22, 2011 at 18:36 UTC
    Why would you expect the count to be more than one, when there's only one instance of what you're matching?

    UPDATE: OP has been edited without notice since this reply and the others to date. Original code (with code tags added by /me) was:

    $cnt =0 $str="hi hi harshmane hi hi"; if ($str=~/([harsh]+)/g) # cf current content { $cnt++; } print $cnt;

    OP has been msgd that such changes without notice are unhelpful.

      I think he did the same thing in to find string. And this question is virtually the same as that one. That might also explain why he only matches once in this thread -- he probably used different data in the other one.

      Elda Taluta; Sarks Sark; Ark Arks
      My deviantART gallery

Re: how to find substring using regex
by wind (Priest) on Jun 22, 2011 at 18:04 UTC
Re: how to find substring using regex
by zek152 (Pilgrim) on Jun 22, 2011 at 17:29 UTC

    Code tags are your (and our) friends.

    Your problem is that you don't store the number of matches. The following code will fix your issue.

    use warnings; use strict; my $cnt = 0; my $str = "hi hi harshmane hi hi"; $cnt = ($str =~ s/(harsh)/$1/g); print "'harsh' appears $cnt times.\n"; $cnt = ($str =~ s/(hi)/$1/g); print "'hi' appears $cnt times.\n"; #OUTPUT #'harsh' appears 1 times. #'hi' appears 4 times.

      No need for the capture and substitution by itself!

      knoppix@Microknoppix:~$ perl -E ' > $str = q{hi hi harshmane hi hi}; > $cnt = () = $str =~ m{hi}g; > say $cnt;' 4 knoppix@Microknoppix:~$

      I hope this is of interest.

      Cheers,

      JohnGG

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to find substring using regex
by hbm (Hermit) on Jun 22, 2011 at 17:55 UTC

    Your code would increment $cnt once if the expression is true (matches one or more times). You could turn it around like this:

    $cnt++ while $str=~/harsh/g;

    Note also that you don't need to store the matches (with parentheses) for what you are doing.

    And the '+' modifier in your example would match 'harsh' and 'harshhhhhhhhhh'. Is that what you meant?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2023-02-04 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?