Jump to content

Recommended Posts

Posted

HI to all, it's my first post here, and unfortunately i am here to ask your help >_<

My question is: there is a way to check if a string contain a given substrings?

I know that there is StringInStr to check if a string contains a given substring. But for example if i have to check if a string contain a group of given substring how can i do?

I want to do something like an AND search with SQL.

I hope you will understand me, for example i have this string.

$string = look_how_much_cool_i_am_is_thatTrue?

Now i want know if this string contain this substrings: "look", "cool", "True".

Of course i can use something like

If StringInStr ($string, "look") AND StringInStr($string, "cool") AND AND StringInStr($string, "True") Then
;Do something
EndIf

But how i can do that dinamically? Of course i dont know what substrings the user want to seach.

I hope that you have understand me. Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Posted

Looks like a job for regular expressions. Please see function StringRegExp.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Maybe something like this?

$string = "look_how_much_cool_i_am_is_thatTrue?"
$searchstring1 = "look"
$searchstring2 = "cool"
$searchstring3 = "true"
If StringInStr($string, $searchstring1) And StringInStr($string, $searchstring2) And StringInStr($string, $searchstring3) Then
    ;Do something
EndIf
; or something like this
$searchstring1 = InputBox("Search string 1", "Enter the first search string")
$searchstring2 = InputBox("Search string 2", "Enter the second search string")
$searchstring3 = InputBox("Search string 3", "Enter the third search string")
If StringInStr($string, $searchstring1) And StringInStr($string, $searchstring2) And StringInStr($string, $searchstring3) Then
    ;Do something
EndIf

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

  Reveal hidden contents

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

There are several ways to do this. If you know exactly which words to test, then you could use a simple approach by looping through an array of matches. If you use RegExp then you must realise that underscore is considered a word character. You could always replace it with a space character. I mention this because you should be careful to avoid finding unwanted substrings within words.

Posted

my effort:

$flag = 0
$string = "look_how_much_cool_i_am_is_thatTrue?"

$input = inputbox ("string search" , "seperate strings with commas" , "look,cool,true")

$sArray = stringsplit ($input , ",")

For $i = 1 to $sArray[0]
If stringinstr($string , $sArray[$i]) Then
$flag += 0
Else
$flag += 1
EndIf
Next


If $flag = 0 Then
msgbox (0, '' , "strings all match")
Else
msgbox (0, '' , $flag & " strings do not match")
EndIf

  Reveal hidden contents

Posted (edited)

  On 2/11/2013 at 10:48 PM, 'BrewManNH said:

Maybe something like this?

$string = "look_how_much_cool_i_am_is_thatTrue?"
$searchstring1 = "look"
$searchstring2 = "cool"
$searchstring3 = "true"
If StringInStr($string, $searchstring1) And StringInStr($string, $searchstring2) And StringInStr($string, $searchstring3) Then
    ;Do something
EndIf
; or something like this
$searchstring1 = InputBox("Search string 1", "Enter the first search string")
$searchstring2 = InputBox("Search string 2", "Enter the second search string")
$searchstring3 = InputBox("Search string 3", "Enter the third search string")
If StringInStr($string, $searchstring1) And StringInStr($string, $searchstring2) And StringInStr($string, $searchstring3) Then
    ;Do something
EndIf

@BrewManNH

Thanks for your help but of course there is not so simple. I don't know how much search strings the user want to compare with the "main string". So i can't hardwrite a function with:

If StringInStr($string, $searchstring1) And StringInStr($string, $searchstring2) And StringInStr($string, $searchstring3) Then
;Do what you want
EndIf

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Posted

A regular expression would be easy.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Well, the search routine would have to do something depending upon whether you want it to do something on 1 or more matches or if you want it to only do something if all search strings are found.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

  Reveal hidden contents

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

With regular expressions it could look like the following code. The first example just returns 0 or 1 if none or any of the search words were found. The second example returns an array of all found words.

#include <Array.au3>
Global $sString = "This is the string I want to test for multiple words"
Global $sWords = "(string|text|words)"
Global $iResult = StringRegExp($sString, $sWords, 0)
ConsoleWrite($iResult & " " & @error)
Global $aResult = StringRegExp($sString, $sWords, 3)
_ArrayDisplay($aResult)
Just concatenate all the search words and give it a try. Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

water's example is case-sensitive (by default) so it won't match String but it will match string. Though you can't turn this off if you so wish.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

To make it case insensitive use the following line:

Global $sWords = "(?i)String|Text|WORDS"

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

  On 2/11/2013 at 11:07 PM, 'water said:

With regular expressions it could look like the following code. The first example just returns 0 or 1 if none or any of the search words were found. The second example returns an array of all found words.

#include <Array.au3>
Global $sString = "This is the string I want to test for multiple words"
Global $sWords = "(string|text|words)"
Global $iResult = StringRegExp($sString, $sWords, 0)
ConsoleWrite($iResult & " " & @error)
Global $aResult = StringRegExp($sString, $sWords, 3)
_ArrayDisplay($aResult)
Just concatenate all the search words and give it a try.

Your regex give me only the search string included in the "main string". But i want that if all the search string are included in the "main string" then do something. So for example if i have 4 search words, i need that all the search worlds were found in the "main string", and so on.

By the way thanks for your help!

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Posted

I tweaked water's code a bit. I imagine the syntax can be improved.

#include <Array.au3>
Global $sString = "This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
$sString = StringReplace($sString, "_", " ") ; Get rid of underscore
Global $sWords = "(?i)\b(string|text|words)\b" ; Case insensitive search between word boundries.
Global $iResult = StringRegExp($sString, $sWords, 0)
ConsoleWrite($iResult & " " & @error & @LF)
Global $aResult = StringRegExp($sString, $sWords, 3)
_ArrayDisplay($aResult)
Posted

I'm confused as to what the OP wants? What water created was how I understood the OP's problem.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Another example:

I have this string:

$string = "This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"

Now the user want to know if all these string (for example: "This", "string", "multiple", "Words") are contain in the $string.

Now if all the strings are contained in the $string do something, so give a warning.

All clear now?

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Posted

  On 2/11/2013 at 11:26 PM, 'Nessie said:

...i want that if all the search string are included in the "main string" then do something. So for example if i have 4 search words, i need that all the search worlds were found in the "main string", and so on.

I understand what you want. You can test the number of returned strings by testing the size of the array using Ubound. This method may not be best. Thinking!

Posted

I re-read it again.

Local $sString = 'This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext'
$sString = StringReplace($sString, '_', ' ') ; Get rid of underscore
Local Const $sWords = '(?i)\b(string|text|words)\b' ; Case-insensitive search between word boundries.

Local Const $WORD_MAX = 3
Local Const $fResult = UBound(StringRegExp($sString, $sWords, 3)) = $WORD_MAX ; Number of words.
ConsoleWrite($fResult & @LF)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Global $sTest = "This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
Global $sPattern = "string|text|words"

If _AllWordsExist($sTest, $sPattern, 3) Then
    MsgBox(0, "", "All words found")
Else
    MsgBox(0, "", "Not all words were found")
EndIf

Func _AllWordsExist($sString, $sWords, $iTrigger)
    $sString = StringReplace($sString, "_", " ") ; Get rid of underscore
    $sWords = "(?i)\b(" & $sWords & ")\b" ; Case insensitive search between word boundries.
    Local $aResult = StringRegExp($sString, $sWords, 3)

    Local $bReturn = False
    If UBound($aResult) >= $iTrigger Then $bReturn = True
    Return $bReturn
EndFunc

Posted

  On 2/12/2013 at 12:06 AM, 'czardas said:

Global $sTest = "This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
Global $sPattern = "string|text|words"

If _AllWordsExist($sTest, $sPattern, 3) Then
    MsgBox(0, "", "All words found")
Else
    MsgBox(0, "", "Not all words were found")
EndIf

Func _AllWordsExist($sString, $sWords, $iTrigger)
    $sString = StringReplace($sString, "_", " ") ; Get rid of underscore
    $sWords = "(?i)\b(" & $sWords & ")\b" ; Case insensitive search between word boundries.
    Local $aResult = StringRegExp($sString, $sWords, 3)

    Local $bReturn = False
    If UBound($aResult) >= $iTrigger Then $bReturn = True
    Return $bReturn
EndFunc

That can be optimised to this ...

#include <Constants.au3>

; Though these are in "Global scope" they are actually used as Local. $sText isn't found anywhere else apart from where it was defined. Same for $sPattern.
Global $sTest = "This_is_the_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
Global $sPattern = "string|text|words"

If _AllWordsExist($sTest, $sPattern, 3) Then
    MsgBox($MB_SYSTEMMODAL, "", "All words found")
Else
    MsgBox($MB_SYSTEMMODAL, "", "Not all words were found")
EndIf

Func _AllWordsExist($sString, $sWords, $iTrigger)
    $sString = StringReplace($sString, "_", " ") ; Get rid of underscore
    $sWords = "(?i)\b(" & $sWords & ")\b" ; Case insensitive search between word boundries.
    Return UBound(StringRegExp($sString, $sWords, 3)) >= $iTrigger
EndFunc   ;==>_AllWordsExist

But if the string is this...

Global $sTest = "This_is_the_string_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
then it will return True when it's false.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 2/12/2013 at 12:12 AM, 'guinness said:

But if the string is this...

Global $sTest = "This_is_the_string_string_I_want_to_test_for_multiple_Words_excluding_the_substring_in_pretext"
then it will return True when it's false.

So this function is buggy? For you what is the best way to do what i want?

Hi! and thanks for the help! ;)

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

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
  • Recently Browsing   0 members

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