Jump to content
Sign in to follow this  
gerwim

Search Strings

Recommended Posts

gerwim

Hi guys,

So, let's say I have this string: "<OPTION value=6>Test</OPTION>".

How can I get the number "6"? There's lot more <options> in there normally. And I want to search for "Test" (so by searching the string for Test, I want 6 as return).

Thanks in advance!

Share this post


Link to post
Share on other sites
saywell

The elegant way is by using a regular expression, but they're hard!

Try _StringBetween - using = and >Test as the delimiters. That's assuming there aren't any other = signs in the line.

William

Share this post


Link to post
Share on other sites
gerwim

The elegant way is by using a regular expression, but they're hard!

Try _StringBetween - using = and >Test as the delimiters. That's assuming there aren't any other = signs in the line.

William

There will be like 40 options in the complete string.. =(

Share this post


Link to post
Share on other sites
gerwim

heres one way

#include <String.au3>

$s = "<OPTION value=6>Test</OPTION>"

If StringInStr($s,"Test",1) Then
    $return = _StringBetween($s,"=",">")
EndIf
MsgBox(0,"",$return[0])

Thanks John, but this will not work due to be more then one option. The complete string will be something like this:

<OPTION value=6>Test</OPTION><OPTION value=7>Test1</OPTION><OPTION value=8>Derp</OPTION><OPTION value=9>Test3</OPTION><OPTION value=10>Blabla4</OPTION>
Edited by gerwim

Share this post


Link to post
Share on other sites
JohnOne

Then the rexexp suggestion above probably the best, I sheepishly work around regexp though.

You could do it many different ways using string functions.

heres an option using native string* functions

$s = "<OPTION value=6>Test</OPTION><OPTION value=7>Test1</OPTION><OPTION value=8>Derp</OPTION><OPTION value=9>Test3</OPTION><OPTION value=10>Blabla4</OPTION>"

$a = StringSplit($s,"<OPTION value=",3)

$searchstring = "Blabla4"

For $i = 0 To Ubound($a) -1
If StringInStr($a[$i],$searchstring,1) Then
    $return = StringTrimRight($a[$i],10+StringLen($searchstring))
EndIf
Next
MsgBox(0,"",$return)

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
Melba23

gerwim,

Do you want just the single digit preceding the "Test" or an array of the digits preceding all the "Test?" elements? :)

M23

Edit: Easier than I thought:

#include <Array.au3>

$sString = "<OPTION value=6>Test</OPTION><OPTION value=7>Test1</OPTION><OPTION value=8>Derp</OPTION><OPTION value=9>Test3</OPTION><OPTION value=10>Blabla4</OPTION>"

$iDigit = StringRegExpReplace($sString, "(?i).*value=(\d+)>Test<.*", "$1")

MsgBox(0, "Result", $iDigit)

$aArray = StringRegExp($sString, "(?i)value=(\d+)>Test", 3)

_ArrayDisplay($aArray)

And my brain did not hurt too much! :)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gerwim

Then the rexexp suggestion above probably the best, I sheepishly work around regexp though.

You could do it many different ways using string functions.

heres an option using native string* functions

$s = "<OPTION value=6>Test</OPTION><OPTION value=7>Test1</OPTION><OPTION value=8>Derp</OPTION><OPTION value=9>Test3</OPTION><OPTION value=10>Blabla4</OPTION>"

$a = StringSplit($s,"<OPTION value=",3)

$searchstring = "Blabla4"

For $i = 0 To Ubound($a) -1
If StringInStr($a[$i],$searchstring,1) Then
    $return = StringTrimRight($a[$i],10+StringLen($searchstring))
EndIf
Next
MsgBox(0,"",$return)

Thanks, will look into that!

gerwim,

Do you want just the single digit preceding the "Test" or an array of the digits preceding all the "Test?" elements? :)

M23

No, just single digit, non array.

Share this post


Link to post
Share on other sites
Melba23

gerwim,

Our posts crossed - see my edit above! :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gerwim

gerwim,

Our posts crossed - see my edit above! :)

M23

Damn, awesome. Gotta learn regexes.. Thanks!

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  

×