Count matching string from clipboard

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Count matching string from clipboard

Post by Danton » Fri Nov 11, 2011 12:25 am

Hi all,

the script I'm writing needs to count the number of matching "strings" in clipboard.

so how do I search the contents of the clipboard and return the number of matches for a string eg number of times "green rabbit" appears in the clipboard.

Thanks :D

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Nov 11, 2011 3:48 am

There are probably other ways but I would simply get the clipboard text and then use Separate> to get a count. The "count" variable provided by Separate> will be one greater than the number of instances.

Code: Select all

GetClipBoard>strText
Separate>strText,green rabbit,item
Sub>item_count,1
MDL>%item_count% "green rabbit" found

Danton
Newbie
Posts: 11
Joined: Thu Aug 26, 2010 12:04 am

Post by Danton » Fri Nov 11, 2011 6:04 am

Thanks, that did the trick!

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sun Nov 13, 2011 8:40 pm

Hi Danton and JRL,

The above will count all occurrances of "green rabbit" but will miss:

Green rabbit
GREEN RABBIT
...etc

If you want to count them all no matter what case was used, you could modify JRL's code like this:

Code: Select all

GetClipBoard>strText
LowerCase>strText,lower_strText
Separate>lower_strText,green rabbit,item
Sub>item_count,1
MDL>%item_count% "green rabbit" found
Another way to do this would be as follows:

Code: Select all

GetClipBoard>strText

//Case In-Sensitive match
Let>pattern=green rabbit

//Case Sensitive match
//Let>pattern=(?-i)green rabbit

RegEx>pattern,strText,0,matches,num,0
MDL>%num% "%pattern%" found
Used as it is, the above will be case in-sensitive but you can use the other pattern if you actually want a case-sensitive match.

Another benefit to this method is... if you are doing a case-insensitive match and you'd like to see the actual strings you matched, they will be stored in variables MATCHES_1, MATCHES_2, etc. whereas they are not stored using the Separate> method.

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts