How to parse a string?

S

snoopy_

If I have a string that looks like this:

]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart

And I want to count all the instances of "GE" how can I do this?

If I perform a simple search with regular expressions I just get one
instance, I want all. I tried to split on GE, then reduce total count
by 1, but this was not reliable. Is there an easy way to do this?

Thanks.
 
A

anno4000

If I have a string that looks like this:

]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart

And I want to count all the instances of "GE" how can I do this?

If I perform a simple search with regular expressions I just get one
instance, I want all.

That works if done correctly. Since you don't show what you have done,
I don't know what you did wrong. Show your code.
I tried to split on GE, then reduce total count
by 1, but this was not reliable.

Have you read the documentation about split(), and what it has to say
about dropping trailing empty fields? And how the LIMIT argument can
be used to keep all fields?
Is there an easy way to do this?

Using split:

$_ = ']^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart';
-- ( my $count =()= split /GE/, $_, -1);

The regex solution is similar but simpler. You can figure it out.

Anno
 
D

DJ Stunks

If I have a string that looks like this:

]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart

And I want to count all the instances of "GE" how can I do this?

If I perform a simple search with regular expressions I just get one
instance, I want all. I tried to split on GE, then reduce total count
by 1, but this was not reliable. Is there an easy way to do this?

Thanks.

perldoc -q occurrences

-jp
 
J

Jürgen Exner

If I have a string that looks like this:

]^Hello]^3422]^GE]^Five]^GE]^Hippo]^GE]^Lart

And I want to count all the instances of "GE" how can I do this?

If I perform a simple search with regular expressions I just get one
instance, I want all.

That's strange. I just wrote a 2 line test program and it correctly printed
3 when using a simple RE on your sample data.
I have a few suspicions why your test program didn't print 3 but there is
little point of confusing you with wild guesses, therefore I am afraid you
will have to post your sample program after all.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top