Jump to content
Sign in to follow this  
Champak

StringRegExp: Trying to grab specific words in a string

Recommended Posts

Champak

I'm trying to get stringregexp to find specific FULL words in my string. My issue is it's only grabbing the last word in the string, and it's grabbing anything that starts with the word in the string. Ex. It grabs BP but also grabs BPS. Help please.

#include <Array.au3>

Local $FULL_GASSTATION_LIST[16][4] = [ _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["B", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["Sunoco", "SubString1=====", "2String0", "2SubString0"], _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BP", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["Sun", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BPS", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["String5", "SubString5", "2String0", "2SubString0"]]

Local $PreferredList[2]

$PreferredList[0] = "Sunoco"
$PreferredList[1] = "BP"

;_ArrayDisplay($FULL_GASSTATION_LIST)
;_ArrayDisplay($PreferredList)

$PreferredList = _ArrayToString($PreferredList, "|")

        For $I = UBound($FULL_GASSTATION_LIST) - 1 To 0 Step -1
                If StringRegExp($FULL_GASSTATION_LIST[$I][0], "(\w+\" & $PreferredList & ")") Then
                    ConsoleWrite("---- " & StringRegExp($FULL_GASSTATION_LIST[$I][0], $PreferredList) & "----  ")
                    ConsoleWrite($I & @CRLF)
                EndIf
        Next

Share this post


Link to post
Share on other sites
JohnOne

StringRegExp returns an array.

Local $FULL_GASSTATION_LIST[16][4] = [ _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["B", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["Sunoco", "SubString1=====", "2String0", "2SubString0"], _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BP", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["Sun", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BPS", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["String5", "SubString5", "2String0", "2SubString0"]]

If $FULL_GASSTATION_LIST Then
    MsgBox(0, 0, 0)
EndIf
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Champak

It returns an array if you specify it to do so, otherwise it returns a 1 if it finds what you are looking for, which is what I'm going for.

Share this post


Link to post
Share on other sites
Iczer

try this:

#include <Array.au3>

Local $FULL_GASSTATION_LIST[16][4] = [ _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["B", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["Sunoco", "SubString1=====", "2String0", "2SubString0"], _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BP", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["Sun", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BPS", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["String5", "SubString5", "2String0", "2SubString0"]]

Local $PreferredList[2]

$PreferredList[0] = "Sunoco"
$PreferredList[1] = "BP"

;_ArrayDisplay($FULL_GASSTATION_LIST)
;_ArrayDisplay($PreferredList)

$PreferredList = _ArrayToString($PreferredList, "\z|\A")

        For $I = UBound($FULL_GASSTATION_LIST) - 1 To 0 Step -1
                ;If StringRegExp($FULL_GASSTATION_LIST[$I][0], "(\w+\" & $PreferredList & ")") Then
                If StringRegExp($FULL_GASSTATION_LIST[$I][0], "(\A" & $PreferredList & "\z)") Then
                    $a = StringRegExp($FULL_GASSTATION_LIST[$I][0], $PreferredList,3)
                    _ArrayDisplay($a)
                    ;ConsoleWrite("---- " & $a & "----  ")
                    ConsoleWrite($I & @CRLF)
                EndIf
        Next

Share this post


Link to post
Share on other sites
Champak

Thanks, that works perfectly However, I'm trying to understand the need to add the a and z as deliminaters. Why isn't a pipe good enough?

Share this post


Link to post
Share on other sites
Beege

The a and z are anchors for your regexpression. A for start of string and z for end of string. The arraytostring($items, 'z|A') results in item[1]z|Aitem[2] and then the first and last anchors are added in the expression now giving  -> Aitem[1]z|Aitem[2]z

Share this post


Link to post
Share on other sites
mikell

You can use the pipe only, but if so you need to specify word boundaries in the expression

#include <Array.au3>

Local $FULL_GASSTATION_LIST[17][4] = [ _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["B", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["Sunoco", "SubString1=====", "2String0", "2SubString0"], _
        ["String0", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BP", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["Sunoco", "SubString1=====", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["Sun", "SubString0", "2String0", "2SubString0"], _
        ["String1", "SubString1=====", "2String0", "2SubString0"], _
        ["BPS", "SubString1=====", "2String0", "2SubString0"], _
        ["String3", "SubString3", "2String0", "2SubString0"], _
        ["String4", "SubString1=====", "2String0", "2SubString0"], _
        ["String5", "SubString5", "2String0", "2SubString0"]]

Local $PreferredList[2], $txt = ""
$PreferredList[0] = "Sunoco"
$PreferredList[1] = "BP"

$PreferredList = _ArrayToString($PreferredList, "|")

        For $I = 0 to UBound($FULL_GASSTATION_LIST) - 1
                If StringRegExp($FULL_GASSTATION_LIST[$I][0], "(\b" & $PreferredList & "\b)") Then
                    $txt &= $I & " : " & $FULL_GASSTATION_LIST[$I][0] & @crlf
                EndIf
        Next
msgbox(0,"", $txt)
Edited by mikell

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.