 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Benny-D Guest
|
Posted: Wed Jan 23, 2008 5:29 pm Post subject: displaying the number of occurances in a string |
|
|
Which command will return the number of times a certain word occurs in a string?
For example, if the string is "A cat met another cat today and went to see yet another cat"
and the given word is "cat", then the number shown is "3". |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Wed Jan 23, 2008 5:59 pm Post subject: |
|
|
| Code: | str=A cat met another cat today and went to see yet another cat
StringReplace, Str,Str,cat,cat, UseErrorLevel
strCnt := Errorlevel
MsgBox, % strCnt |
 |
|
| Back to top |
|
 |
Benny-D Guest
|
Posted: Wed Jan 23, 2008 6:06 pm Post subject: |
|
|
| SKAN, thank you!!! |
|
| Back to top |
|
 |
dmatch
Joined: 15 Oct 2007 Posts: 113
|
Posted: Wed Jan 23, 2008 6:07 pm Post subject: |
|
|
Here is another way so you can choose your poison:
| Code: | TheString=A cat met another cat today and went to see yet another cat
pos:=1
Loop
{
If(pos:=Instr(TheString,"cat",0,pos)){
count++
pos+=3
}
Else
break
}
msgbox,Found cat %count% times. |
dmatch |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5068 Location: imaginationland
|
Posted: Wed Jan 23, 2008 6:14 pm Post subject: |
|
|
StringReplace is good for finding instances of any string, but if you need to find words RegEx may be more appropriate:
| Code: | str = Cat: A small carnivorous mammal domesticated since early times as a rat catcher
StringReplace, str, str, cat, cat, UseErrorLevel
MsgBox, String-replace method found: %ErrorLevel%
p = 0
Loop
If (RegExMatch(str, "i)\bcat\b", "", ++p))
C++
Else Break
MsgBox, Regex method found: %c% |
_________________
RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Benny-D Guest
|
Posted: Wed Jan 23, 2008 6:36 pm Post subject: |
|
|
| Titan wrote: | | str = Cat: A small carnivorous mammal domesticated since early times as a rat catcher |
This is really something - the definition of the word cat has 3 "cats" in it!
Thank you, Titan |
|
| Back to top |
|
 |
Benny-D Guest
|
Posted: Fri Jan 25, 2008 3:52 pm Post subject: |
|
|
How do I make it show me in a message box all the numbers in the string, one by one, that are less than 87?
Here is the string:
256 cities in 3 counties of this 1 country voted for 12 candidates in each district, among which only 5 could be elected and be included in the group 99, as well as in the group 87 and the ward 87
This code only shows me how many times number 87 occurs in the string:
| Code: | StringReplace, Str,Str,87, 87, UseErrorLevel
strCnt := Errorlevel
MsgBox, % strCnt |
|
|
| Back to top |
|
 |
Mustang
Joined: 17 May 2007 Posts: 375 Location: England
|
Posted: Sat Jan 26, 2008 4:04 am Post subject: |
|
|
| Code: | String := "256 cities in 3 counties of this 1 country voted for 12 candidates in each district, among which only 5 could be elected and be included in the group 99, as well as in the group 87 and the ward 87"
StartPosition=1
NumberFound:=""
NumbersLessThan87:=""
Loop
{
SearchPosition := StartPosition+StrLen( NumberFound )
If ( SearchPosition <= StrLen( String ) )
{
StartPosition := RegExMatch( String, "[0-9]+", NumberFound, SearchPosition )
If ( ( NumberFound < 87 ) And ( NumbersLessThan87 = "" ) )
{
NumbersLessThan87 := NumberFound
}
Else If ( ( NumberFound < 87 ) And ( NumbersLessThan87 != "" ) )
{
NumbersLessThan87 := NumbersLessThan87 . "`n" . NumberFound
}
}
Else
{
Break
}
}
MsgBox, %NumbersLessThan87% |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Sat Jan 26, 2008 4:25 am Post subject: |
|
|
| Titan wrote: | | to find words RegEx may be more appropriate | Good point. You could avoid loops, though, by using the OutputVarCount parameter of RegExReplace: “RegExReplace(str, "i)\bcat\b", "", OutputVarCount)” |
|
| Back to top |
|
 |
Benny-D Guest
|
Posted: Sat Jan 26, 2008 8:29 am Post subject: |
|
|
| Mustang, thank you very much!!! |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5068 Location: imaginationland
|
Posted: Sat Jan 26, 2008 11:29 am Post subject: |
|
|
| Laszlo wrote: | | You could avoid loops, though, by using the OutputVarCount parameter of RegExReplace | That's great! How did I miss that  _________________
RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Sat Jan 26, 2008 6:18 pm Post subject: |
|
|
Here is a loop-less version of Mustang’s script, which lists all numbers between 0 and 86 in a string (no leading zeros). It attaches x1 to the string to ensure a match at the end, to be removed later. This way the regular expression can be simpler. It just matches single digits and two digit numbers less than 87, surrounded by non-digits. | Code: | String := "256 cities in 3-4 counties of this 1 country voted for 12 candidates in each district, "
. "among which only 5 could be elected and be included in the group 99- as well as in the group 87 "
. "and the ward 87"
t := RegExReplace(String . "x1", "(.*?)(^|\D)(\d|[1-7]\d|8[0-6])(?=\D|$)", "$3,")
t := SubStr(t,1,StrLen(t)-3) ; remove helper ",1," from end
MsgBox %t% | It can be further simplified, or handle leading 0’s, but you get the idea… |
|
| Back to top |
|
 |
Benny-D Guest
|
Posted: Sun Jan 27, 2008 7:30 pm Post subject: |
|
|
| WOW!!! Laszlo, thank you!!! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|