Jump to content

Find same word multiple times in a string


 Share

Recommended Posts

For some reason I'm missing this one and I shouldn't be, I can't see the trees for the forest in this case :)

Someone asked me to look for certain key words in a code review of some websites that we manage, (possibly millions of lines), I created a test script by using stringregexp, stringinstr, stringsplit and I can find the key words but not if it appears on the same line, so for example if I'm looking for the string "form" in

<form id="form 1" runat="server">

the first "form" is found and counted but not the second one and so on, so the count is not accurate. It's the same for every line that is searched. It's like as soon as there is a match, the script moves to the next line.

How does one find all occurances in a sting?

Link to comment
Share on other sites

StringInStr only finds the first instance of form for me, there must be something else to it. Maybe it's with my count

I'm doing something like this.

$count = 0

$findit = StringInStr($text, "form")

if $findit <> 0 then

$count = $count + 1

endif

Link to comment
Share on other sites

Use the optional params for StringInStr:

From helpfile:

StringInStr

---------------------------------------------------------

Checks if a string contains a given substring.

StringInStr ( "string", "substring" [, casesense [, occurrence]] )

---------------------------------------------------------

You wil need to search for all values of 'occurence'

Link to comment
Share on other sites

The following code should do it, however I'm afraid it will also count anything that contains "form" as a substring, not only as word...

StringReplace($txt, "form", "form")

MsgBox(0, "Number of occurences", @extended)

Why crabs don't give money to charity..... because they are shell-fish!! PS: Don't be a crab and share your scripts with the community! ;-)
Link to comment
Share on other sites

Maaaan, I went about this the hard way, I tend to do that. I can see where StringReplace would work but it's not quiet what I want.

This does what I want,

$text = "my search word"
While blah blah blah
$line = FileReadLine($targetfile)
$findText =  StringRegExp($line,$text,3)
$count = UBound($findText)
$count1 = $count1+$count
wend

Thanks again Weaponx, that's twice you saved me with Ubound, I really gotta start using it more.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...