Jump to content

StringRegExp Question


Recommended Posts

Hello!

Very new to AutoIt so please pardon my ignorance. I'm trying to sort through some text on the clipboard and the text that i'm looking for is always in a particular pattern.

+xx to Charisma

StringRegExp($clipboard, 'to Charisma') works, but i'm trying to only get a match when xx is above a certain value.

Can anyone point me in the right direction? I've tried searching but I can't seem to find what I need.

Thank you

Link to comment
Share on other sites

 go ahead and call them widgets everytime instead of game related attributes (as we dont do gaming scripts round here).    you can get just the numbers, test them, and then return the entire string...is that the intent?

If you just want the boolean return thats cool, but working backwards from the full return might help a bit.

;~ $sTest = "+200 to widgets" ;match due to greater operator
;~ $sTest = "+19 to widgets" ;match due to = operator
;~ $sTest = "+18 to widgets" ;no match less than min


$sPattern = "\+(\d+) to widgets" ;this is assuming all strings have a + sign in them, if there is a chance of a '-' or no operator this expression would fail
$min = 19

$x = number(stringregexp($sTest , $sPattern , 3)[0]) >= $min ? stringregexp($sTest , "(" & $sPattern & ")" , 3)[0] : "<does not match criteria>"

msgbox(0, '' , $x)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Wow, thank you so much.  This will help me accomplish my goal. Do you mind if i ask some questions about the function and definitions in AutoIT?

$sPattern = "\+(\d+) to charisma"


When defining the pattern, +(\d+) is equal to +xx where xx is any decimal, correct? What does the first in the pattern represent? Will adding a (?i) after make any following text after in the pattern case insensitive? 

Thanks again for your help!

 

Link to comment
Share on other sites

$x = number(stringregexp($sTest , $sPattern , 3)[0]) >= $min ? stringregexp($sTest , "(" & $sPattern & ")" , 3)[0] : "<does not match criteria>"

This code works  great, but I don't understand why. I'm trying to make sense of it in the documentation but i'm not getting anywhere. Am I on the right track?

Is the  flag 3 returning the match to an array then then 'number(stringregexp' isolates the numbers and checks if it >= $min ?


After that I'm lost.

 

Link to comment
Share on other sites

The first backslash escapes the plus sign (if you change that to a negative the direct array call throws an exception because no array was returned from the regex). The capture is any digit with the unescaped plus sign meaning one or more.  The 3 returns an array of global matches so the [0] after it is returning the first element of that array.

it could be set to insensitive, or it could be '\Dharisma', lots of choices depending on the variety of input

 

The ternary says 

check that number ? If true return the whole match because I stick that whole thing in another set of parentheses : else whatever you want when it fails the match

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Another way, scanning the subject string only once:

Local $s = "fhui +17 to Charisma -25 to fhkl +43 to me +51 to Charisma +16 to Charisma"
Local $max = 0
$max = Int(Execute(StringRegExpReplace($s, "(?:.*?)\+(\d\d) to Charisma", "($1 > $max ? - $max + Assign('max', $1) - 1 + $max : 0) + ") & "0"))

ConsoleWrite($max & @LF)

A rare use of Assign().

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...