Jump to content
Deon

Get ajacent text

Recommended Posts

Deon

Hi,

I am looking for a way to identify text ajacent to a string in another string/window/etc.

Eg:
John - 53
Mary - 12
Bill - 32
Jill - 11

Say, 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 by Deon

Share this post


Link to post
Share on other sites
Malkey

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 by Malkey
Addressing jguinch's observation

Share this post


Link to post
Share on other sites
jguinch

@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 :P

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)

 

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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

×