Jump to content
Sign in to follow this  
sroprogger

Search a word in a text

Recommended Posts

sroprogger

Okay here's my idea

I want to read a specific line of a text document, search it for a specific word and display that word

as a label in my GUI

Now my problem is

The word can differ, it can be 6 different words like: hello,no,yes,maybe,sir,yellow

i did some research and found this

StringRegExp and StringInStr - but they just give me numbers and i dont really know how to use them

The help file doesnt help me at all because i dont understand it :)

well thx for your answers :)

Share this post


Link to post
Share on other sites
TerarinK

$sValue = "Hello, this is a test."
If StringRegExp($sValue, "(?i:hello|no|yes|maybe|sir|yellow)", 0) Then DoWhateverYouWant()


0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Share this post


Link to post
Share on other sites
sroprogger

$sValue = "Hello, this is a test."
If StringRegExp($sValue, "(?i:hello|no|yes|maybe|sir|yellow)", 0) Then DoWhateverYouWant()
well - when i did this i got 1 on success - 0 on failure

Now that CAN help me - but im looking for an easier way since i would have to write 200 lines of code just for one thing using

StringRegExp -because there is actually 200 posibilities -

now is there a way to search a string - lets say 'hello<"name = 77618">' - for the value after 'hello<"name = ... and then save it to

another string ? if so could you please tell me :)

thx for your answers

Share this post


Link to post
Share on other sites
CodyBarrett

Strininstr()

OR

stringtrimleft()  Stringtrimright()

OR

stringreplace()

whatever you need please CHECK THE HELP FILE its your best friend in autoit

Share this post


Link to post
Share on other sites
sroprogger

Strininstr()

OR

stringtrimleft()  Stringtrimright()

OR

stringreplace()

whatever you need please CHECK THE HELP FILE its your best friend in autoit

but all those expressions do is check if that word is in there - is there an expression that can read a specific word at a specific place

and then diplay it onto the gui ?

if not ill have to go with the other expressions - but 200 lines of code x.x

well thx for your answers

Share this post


Link to post
Share on other sites
Inverted

If you know what's before and after the string, then you can use StringBetween. You need to know something about the location of the string, it can't be found magically !

Edited by Inverted

Share this post


Link to post
Share on other sites
Malkey

well - when i did this i got 1 on success - 0 on failure

Now that CAN help me - but im looking for an easier way since i would have to write 200 lines of code just for one thing using

StringRegExp -because there is actually 200 posibilities -

now is there a way to search a string - lets say 'hello<"name = 77618">' - for the value after 'hello<"name = ... and then save it to

another string ? if so could you please tell me :)

thx for your answers

This may give you some ideas.

#include <Array.au3>

; From post 
; http://www.autoitscript.com/forum/index.php?s=&showtopic=89105&view=findpost&p=640323
; - lets say 'hello<"name = 77618' - for the value after 'hello<"name = ... and 
; then save it to another string ?

$sString = 'Extra text here hello<' & '"' & 'name = 77618' & '"' & '> and here' 

; That which is found between the brackets, (), is returned to the array.
; This case: \d is any digit. * means repeat the previous character 0 or more times.
; So this will search the $sString for the text in the regular expression, and will
; return all occurances of the digits, if any, that appear after [hello<"name = ] and 
; before [">]
$array = StringRegExp($sString, 'hello<' & '"' & 'name = (\d*)' & '"' & '>' , 3) 

_ArrayDisplay($array)
$sAnotherString = _ArrayToString($array)
MsgBox(0,"A String",$sAnotherString)


#cs
; Easier to search whole of file instead of one line at a time.Will return an array 
; of all matches.
$sFile = "FullPath\and\FileName.txt" ; Full path if not in same script directory.
$sRegExp = 'hello<' & '"' & 'name = (\d*)' & '"' & '>' 
StringRegExp(FileRead($sFile), $sRegExp , 3)
#ce

Share this post


Link to post
Share on other sites
sroprogger

This may give you some ideas.

#include <Array.au3>

; From post 
; http://www.autoitscript.com/forum/index.php?s=&showtopic=89105&view=findpost&p=640323
; - lets say 'hello<"name = 77618' - for the value after 'hello<"name = ... and 
; then save it to another string ?

$sString = 'Extra text here hello<' & '"' & 'name = 77618' & '"' & '> and here' 

; That which is found between the brackets, (), is returned to the array.
; This case: \d is any digit. * means repeat the previous character 0 or more times.
; So this will search the $sString for the text in the regular expression, and will
; return all occurances of the digits, if any, that appear after [hello<"name = ] and 
; before [">]
$array = StringRegExp($sString, 'hello<' & '"' & 'name = (\d*)' & '"' & '>' , 3) 

_ArrayDisplay($array)
$sAnotherString = _ArrayToString($array)
MsgBox(0,"A String",$sAnotherString)


#cs
; Easier to search whole of file instead of one line at a time.Will return an array 
; of all matches.
$sFile = "FullPath\and\FileName.txt" ; Full path if not in same script directory.
$sRegExp = 'hello<' & '"' & 'name = (\d*)' & '"' & '>' 
StringRegExp(FileRead($sFile), $sRegExp , 3)
#ce

i still dont get it .... ive been trying to understand it for 2 hours , reading the helfile, searching the forum, i just dont get it !

could you maybe do another example using the code i'll be using

so

$a = <option name="HP_LOW" value="85" />

[ find the value after 'value="... ]

Thx for your answers

EDIT::

with the _StringBetween

heres the code im trying to use

$a = '<option name="HP_LOW" value="85" />'
$string_between = _StringBetween($a,'value="','" />')
MsgBox(0,"",$string_between)

but it doesn't work - what am i doing wrong ?

Edited by sroprogger

Share this post


Link to post
Share on other sites
Inverted

#Include <string.au3>

$a = '<option name="HP_LOW" value="85" />'
$string_between = _StringBetween($a,'value="','" />')
MsgBox(0,"",$string_between[0])

Helpfile says :

"Return Value

Success: A 0 based $array[0] contains the first found string.

"

Share this post


Link to post
Share on other sites
Authenticity

#include <String.au3>
$a = '<option name="HP_LOW" value="85" />'
$string_between = _StringBetween($a,'value="','" />')
MsgBox(0,"",$string_between[0])

Read carefully the remarks as well as the parameters and return value.

Share this post


Link to post
Share on other sites
sroprogger

thx to you all !!!!!! thank you so much :)

*happy* i figured it out and its working perfetly now !! its so cool

i love you haha ^^

well thank you for helping me !

Share this post


Link to post
Share on other sites
TerarinK

You could also do this:

$sValue = '<option name="HP_LOW" value="85" />'
StringRegExp($sValue, "(?i)="(\w*)"", 3)

This will give you both HP_LOW and 85 in a array


0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

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.