Jump to content

Search for a string and return that string


Recommended Posts

Hello, I want to be able to search for multiple strings and if any of the strings are found I want it returned which one it is. I've been able to search for multiple strings using the OR command, but I'm not sure how I am supposed to make it return which of my strings is found. I've given an example below

If StringInStr($html, Batman) OR  StringInStr($html, Superman) Then
   MsgBox(0, "Success", "The string found was: Superman")
EndIf

So it will search for the two strings and whichever it founds it will return me with. How can I make that work?

Link to comment
Share on other sites

I think that's a job for StringRegExp. I hope some RegExp guru will chime in to explain which pattern is needed :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

;~ $html = "string about Batman"
$html = "string Superman is in"

$Batman = StringInStr($html, "Batman")
$Superman = StringInStr($html, "Superman")
$sOut = $Batman > 0 ? "Batman: " & $Batman : "Superman: " & $Superman
msgbox(0, '' , $sOut)

 

This assumes there can only be one or the other, never both.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

or make a own func for it:

$sIsIn='Batman|Superman|Spiderman'
$sString='Once a day Batman and Spiderman helped each other. As it was a hard fight against unknown enemy, they called Spiderman'
$sTest=_FindFirstMatch($sString,$sIsIn)
MsgBox(0,'func test',$sTest)
Func _FindFirstMatch($sStr, $sDelArray)
    Local $aIN = StringSplit($sDelArray,'|',3)
    For $i=0 To UBound($aIN)-1
        If StringInStr($sStr,$aIN[$i]) Then return SetError(0,$i,$aIN[$i])
    Next
    Return SetError(1,0,'')
EndFunc

 

Link to comment
Share on other sites

5 minutes ago, iamtheky said:
;~ $html = "string about Batman"
$html = "string Superman is in"

$Batman = StringInStr($html, "Batman")
$Superman = StringInStr($html, "Superman")
$sOut = $Batman > 0 ? "Batman: " & $Batman : "Superman: " & $Superman
msgbox(0, '' , $sOut)

 

This assumes there can only be one or the other, never both.

Worked like a charm thanks! Just curious, but when I am retuned with the value it is followed by a number. What is this number? 

Link to comment
Share on other sites

The string in string return, which is the position of the match.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Local $stringFound = FindString ( "Superman is in" )
ConsoleWrite ( "The string found was: " & $stringFound & @CRLF )

Func FindString ( $sString )
    Local $sReturnValue = ""

        Switch $sString
            Case StringInStr ( $sString, "Superman" ) > 0
                $sReturnValue = "Superman"
            Case StringInStr ( $sString, "Batman" ) > 0
                $sReturnValue = "Batman"
        EndSwitch
        Return $sReturnValue
EndFunc

Using the switch / stringinstr statement. Assuming only one result gets returned

Link to comment
Share on other sites

5 minutes ago, iamtheky said:

The string in string return, which is the position of the match.

Oh alright. Makes sense 

 

4 minutes ago, pluto41 said:
Local $stringFound = FindString ( "Superman is in" )
ConsoleWrite ( "The string found was: " & $stringFound & @CRLF )

Func FindString ( $sString )
    Local $sReturnValue = ""

        Switch $sString
            Case StringInStr ( $sString, "Superman" ) > 0
                $sReturnValue = "Superman"
            Case StringInStr ( $sString, "Batman" ) > 0
                $sReturnValue = "Batman"
        EndSwitch
        Return $sReturnValue
EndFunc

Using the switch / stringinstr statement. Assuming only one result gets returned

That's interesting. But what if I were to have lets say, 20 different substrings. How would I search for 20 different substrings within a string and be returned with the one that occur. (Assuming that there is only one possible of the 20).

Would I just add the other 18 to the Switch?

Edited by AliOzturk
Link to comment
Share on other sites

If we can assume that - Batman or Superman will appear in the string, and only one of them will appear.

Then its one line:

msgbox(0, '' , StringInStr($html, "Batman") ? "Batman": "Superman")

 

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

3 minutes ago, iamtheky said:

If we can assume that - Batman or Superman will appear in the string, and only one of them will appear.

Then its one line:

msgbox(0, '' , StringInStr($html, "Batman") ? "Batman": "Superman")

 

 

Haha wow. That's so much more simple, will it work with multiple strings as well? Let's say 20 different strings to search for instead of the two?

Oh wait... The problem with this is that there doesn't have to be any of them. It's a loop, so it just keeps searching until one of them appears. So this actually doesn't work with what I want, unfortunately 

Edited by AliOzturk
Link to comment
Share on other sites

see, there are rules.

$html = "string about Batman and Asterix"
;~ $html = "string about teen titans go"

local $aSuper = ["Batman" , "Superman" , "Lobo" , "Deadpool" , "Wolverine" , "Colossus" , "Sabertooth" , "Groo" , "Asterix" , "Obelix"]

$sOut = ""

For $i = 0 to ubound($aSuper) - 1
  If stringinstr($html , $aSuper[$i]) Then
     $sOut &= $aSuper[$i] & @LF
  EndIf
Next

msgbox(0, '' , $sOut = "" ? "None Found" : $sOut)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

#include <array.au3>

; Add one or More Strings to the Array $aStringToFind
Local $aStringToFind = ["Superman", "Batman", "Captain America", "The Hulk"]
; _arraydisplay ( $aStringToFind )  ; for debugging purposes

Local $stringFound = FindString ( "Captain America is in" )
ConsoleWrite ( "The string found was: " & $stringFound & @CRLF )

; Func that loops trough the array and returns 
Func FindString ( $sString )
    For $i = 0 to UBound ( $aStringToFind ) - 1
        If StringInStr ( $sString, $aStringToFind[$i] ) > 0 Then
            Return $aStringToFind[$i]   ; String Found, return result.
        EndIf
    Next
    Return ""   ; String not found
EndFunc

Perhaps a Array is the way to go?

Edited by pluto41
Link to comment
Share on other sites

A regexp will do that nicely.

Local $s = "My friend is Superman and has a blue suit"
ConsoleWrite(_FindHero($s) & @LF)
$s = "I'm Catwoman under disguise"
ConsoleWrite(_FindHero($s) & @LF)
$s = "Written as spiDer-Man works as well"
ConsoleWrite(_FindHero($s) & @LF)
$s = "Just as Captain           AmeRiCa"
ConsoleWrite(_FindHero($s) & @LF)
$s = "But not this: Captain-AmeRiCa"
ConsoleWrite(_FindHero($s) & @LF)
$s = "Order matters: batman beats spiderman"
ConsoleWrite(_FindHero($s) & @LF)


Func _FindHero($s)
    Local $a = StringRegExp($s, "(?i)(batman|spider(?:-?)man|superman|catwoman|ironman|captain(?: +)america)", 1)
    Return (@error ? -1 : $a[0])
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

very nice, since regexp plays well with arraytostring, an array makes it all pretty like.

#include<array.au3>

$s = "string about Batman and asterix"

local $aSuper = ["Batman" , "Superman" , "Lobo" , "Deadpool" , "Wolverine" , "Colossus" , "Sabertooth" , "Groo" , "Asterix" , "Obelix"]

_ArrayDisplay(StringRegExp($s, "(?i)" & _ArrayToString($aSuper), 3))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

My approach

$sTest = "Look, up in the sky, it's a bird, it's a plane, it's Superman"
ConsoleWrite(_FindStringInStr($sTest, "Superman"))
ConsoleWrite(@CRLF)
ConsoleWrite(_FindStringInStr($sTest, "Superman|Batman|Spiderman"))

Func _FindStringInStr($sString, $sWords)
    Local $sFind, $sReturn
    If Not StringInStr($sWords, "|") Then ; single item
        $sFind = StringInStr($sString, $sWords)
        If $sFind <> 0 Then
            Return "FOUND: " & StringMid($sString, $sFind, StringLen($sWords)) & @CRLF
        Else
            Return "NOT_FOUND: " & $sWords
        EndIf
    Else ; multiple item
        Local $aWords = StringSplit($sWords, "|", 2)
        For $i = 0 To UBound($aWords) - 1
            $sFind = StringInStr($sString, $aWords[$i])
            If $sFind <> 0 Then
                $sReturn &= "FOUND: " & StringMid($sString, $sFind, StringLen($aWords[$i])) & @CRLF
            Else
                $sReturn &= "NOT_FOUND: " & $aWords[$i] & @CRLF
            EndIf
        Next
        Return $sReturn
    EndIf
EndFunc   ;==>_FindString
FOUND: Superman

FOUND: Superman
NOT_FOUND: Batman
NOT_FOUND: Spiderman

You know what are the string found and what are not found, plus you don't need any complex regex or array

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

And why not strictly the basics, using a func very easy to configure ?

$main_txt = "Superman is in"

Msgbox(0,"", "The found hero was: " & _FindHero($main_txt) )

Func _FindHero($sString)
    Local $sHeroes = "Batman,Superman"   ; comma-separated list of heroes

    Local $aHeroes = StringSplit($sHeroes, ",")
    For $i = 1 to $aHeroes[0]
        If StringInStr($sString, $aHeroes[$i]) Then Return $aHeroes[$i]
    Next
    Return "none"
EndFunc

 

Link to comment
Share on other sites

18 hours ago, mikell said:

And why not strictly the basics, using a func very easy to configure ?

$main_txt = "Superman is in"

Msgbox(0,"", "The found hero was: " & _FindHero($main_txt) )

Func _FindHero($sString)
    Local $sHeroes = "Batman,Superman"   ; comma-separated list of heroes

    Local $aHeroes = StringSplit($sHeroes, ",")
    For $i = 1 to $aHeroes[0]
        If StringInStr($sString, $aHeroes[$i]) Then Return $aHeroes[$i]
    Next
    Return "none"
EndFunc

 

Worked like a charm. Was just what I was looking for actually. Thank you!

Edited by AliOzturk
Link to comment
Share on other sites

 

18 hours ago, mikell said:

And why not strictly the basics, using a func very easy to configure ?

$main_txt = "Superman is in"

Msgbox(0,"", "The found hero was: " & _FindHero($main_txt) )

Func _FindHero($sString)
    Local $sHeroes = "Batman,Superman"   ; comma-separated list of heroes

    Local $aHeroes = StringSplit($sHeroes, ",")
    For $i = 1 to $aHeroes[0]
        If StringInStr($sString, $aHeroes[$i]) Then Return $aHeroes[$i]
    Next
    Return "none"
EndFunc

 

full ACK:

21 hours ago, AutoBert said:

or make a own func for it:

$sIsIn='Batman|Superman|Spiderman'
$sString='Once a day Batman and Spiderman helped each other. As it was a hard fight against unknown enemy, they called Spiderman'
$sTest=_FindFirstMatch($sString,$sIsIn)
MsgBox(0,'func test',$sTest)
Func _FindFirstMatch($sStr, $sDelArray)
    Local $aIN = StringSplit($sDelArray,'|',3)
    For $i=0 To UBound($aIN)-1
        If StringInStr($sStr,$aIN[$i]) Then return SetError(0,$i,$aIN[$i])
    Next
    Return SetError(1,0,'')
EndFunc

 

 

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

×
×
  • Create New...