Deon Posted April 29, 2015 Share Posted April 29, 2015 (edited) Hi,I am looking for a way to identify text ajacent to a string in another string/window/etc.Eg:John - 53Mary - 12Bill - 32Jill - 11Say, I need to know how many Jill has done.. I want to be able to present 'Jill' to the script, and it returns '11'. Possible? Edited April 29, 2015 by Deon Link to comment Share on other sites More sharing options...
Malkey Posted April 29, 2015 Share Posted April 29, 2015 (edited) Here are two methods.#include <Array.au3> Local $sData = "John - 53" & @CRLF & "Mary - 12" & @LF & "Bill - 32" & @LF & "Jill - 11" Local $aArray = StringRegExp($sData, "([^\s\-]+)", 3) ; Find all sequence of characters (left to right) that are not a space, line feeds, nor "-". Local $sFind = "Jill" MsgBox(0, "Result", 'Find "' & $sFind & '".' & @CRLF & 'Next element in the array is "' & $aArray[_ArraySearch($aArray, $sFind) + 1] & '".') ConsoleWrite('Find "' & $sFind & '".' & @CRLF & 'Next element in the array is "' & $aArray[_ArraySearch($aArray, $sFind) + 1] & '".' & @LF) _ArrayDisplay($aArray)And,Local $sData = "John - 53" & @CRLF & "Mary - 12" & @LF & "Bill - 32" & @LF & "Jill - 11" Local $sFind = "Jill" ConsoleWrite('Given "' & $sFind & '", Returns "' & (StringRegExp($sData, "(?i)\b" & $sFind & "\b") ? StringRegExpReplace($sData, "(?is).*\b" & $sFind & "\b\h*-\h*(\d+).*", "\1") : "Name absent") & '"' & @LF)Edit: Replaced "ConsoleWrite('Given "' & $sFind & '", Returns "' & StringRegExpReplace($sData, "(?s).*" & $sFind & "\h*-\h*(\d+).*", "\1") & '".' & @LF)" Edited April 29, 2015 by Malkey Addressing jguinch's observation Link to comment Share on other sites More sharing options...
jguinch Posted April 29, 2015 Share Posted April 29, 2015 @Malkey : your 2nd suggestion fails if I search just "ill", or a part of a name (problem with composed names). It also returns the whole string where the name is not found Here is another regex method (returns 0 if the name is not found) :Local $sData = "John - 53" & @LF & "Mary - 12" & @LF & "Bill - 32" & @LF & "Jill - 11" Local $sSearch = "Bill" Local $iValue = Number ( StringRegExp($sData, "(?i)(?:^|\R)" & $sSearch & "[\h-]+(\d+)|()$", 1)[0] ) ConsoleWrite($iValue) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
UEZ Posted April 29, 2015 Share Posted April 29, 2015 One more:Global $oDict = ObjCreate('Scripting.Dictionary') ;more e.g. here: https://technet.microsoft.com/en-us/library/ee176993.aspx?f=255&MSPPError=-2147217396 DictAdd($oDict, "John", 53) DictAdd($oDict, "Mary", 12) DictAdd($oDict, "Bill", 32) DictAdd($oDict, "Jill", 11) MsgBox(0, "Test", "Jill - " & DictFind($oDict, "Jill")) Func DictAdd($oDict, $vData, $iIndex) If Not $oDict.Exists($vData) Then $oDict.Add($vData, $iIndex) Else Return 0 EndIf EndFunc Func DictFind($oDict, $vData) Return $oDict.Item($vData) EndFunc Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Deon Posted April 29, 2015 Author Share Posted April 29, 2015 Thanks guys Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now