Chromed

StringRegExp

17 posts in this topic

I have tried to look up guides about using StringRegExp and I got what I could from them but could anyone tell me, what do I start the pattern off with if for example in the sentence,

"I had 290 Cats and I lost 50 and now I have 240 "

I want the string to start searching AFTER the word "have" and give me ONLY the number 240. I know you can use " ?: " as a pattern to search before a line of text, I just want to know the reverse code.

Share this post


Link to post
Share on other sites



If you had more examples a better regex could be written, but based on what your saying this simple regex will search after the word have and return only 240.

Would work on any string with that same situation "have X"

 

#Include <Array.au3>

$sString = "I had 290 Cats and I lost 50 now I have 240"

$aResults = StringRegExp($sString, "(?i)have.(\d+)", 3)
_ArrayDisplay($aResults)

 

Share this post


Link to post
Share on other sites
30 minutes ago, ViciousXUSMC said:

If you had more examples a better regex could be written, but based on what your saying this simple regex will search after the word have and return only 240.

Would work on any string with that same situation "have X"

 

#Include <Array.au3>

$sString = "I had 290 Cats and I lost 50 now I have 240"

$aResults = StringRegExp($sString, "(?i)have.(\d+)", 3)
_ArrayDisplay($aResults)

 

Thank you, I appreciate the help!

Share this post


Link to post
Share on other sites

There are obviously many ways to skin these 240 cats and as ViciousXUSMC noticed the best one depends on the precise context  :)

Share this post


Link to post
Share on other sites
1 hour ago, Chromed said:

I have tried to look up guides about using StringRegExp and I got what I could from them but could anyone tell me, what do I start the pattern off with if for example in the sentence,

"I had 290 Cats and I lost 50 and now I have 240 "

I want the string to start searching AFTER the word "have" and give me ONLY the number 240. I know you can use " ?: " as a pattern to search before a line of text, I just want to know the reverse code.

Is there a way to have StringRegExp work backwards? so for example if the sentences were

"I had 290 Cats and I lost 50 and now I have 240 ,

I lost 230 Dogs and I now have 10 "

Usually, it would only output 240 since it outputs the first match, what If I wanted it to output 10, would I have to find a way to reverse the search?

Share this post


Link to post
Share on other sites

Sorry I meant to specify, I want it to output only the number 10 not the previous match.

Share this post


Link to post
Share on other sites

Chromed - There is a tool in the autoIt helpfile that you will love. It is a GUI script that helps with StringRegExp. Let me see if I can find where it is in the Help file....

:think:

.......

(few minutes later...)

Took me a few minutes to find it but look in the help file for "Tutorial - Regular Expression" then scroll to the bottom of the page and you will see a button called "Open StringRegExp.au3". Open it in SciTE then run it. That should help you.

Share this post


Link to post
Share on other sites

jguinch, it's because you didn't use the string from post #5   :)

#Include <Array.au3>

$sString =  "I had 290 Cats and I lost 50 and now I have 240, " & @crlf & _ 
            "I lost 230 Dogs and I now have 10 "

$aResults = StringRegExp($sString, "(?s).+ have (\d+)", 1)
_ArrayDisplay($aResults)

 

Share this post


Link to post
Share on other sites
45 minutes ago, mikell said:

jguinch, it's because you didn't use the string from post #5   :)

#Include <Array.au3>

$sString =  "I had 290 Cats and I lost 50 and now I have 240, " & @crlf & _ 
            "I lost 230 Dogs and I now have 10 "

$aResults = StringRegExp($sString, "(?s).+ have (\d+)", 1)
_ArrayDisplay($aResults)

 

Thank you this is giving me the correct results, now to make this go further, If I want the script to loop but I don't want it to catch previous matches its made, e.g 240, 10. What would pattern or code what I have to implement? maybe turn it into an array of sorts? So if the sentence was

"I had 290 Cats and I lost 50 and now I have 240,
I lost 230 Dogs and I now have 10."

But if I leave it looping, I don't want it to keep giving me 10 or 240 and only give me a match when the following sentence is added..

"I purchased 153 Lizards and some ran away so now I have 125"

then it should match the number 125 and then keep looping until another number after the word "have" appears.

Share this post


Link to post
Share on other sites
1 hour ago, Chromed said:

Thank you this is giving me the correct results, now to make this go further, If I want the script to loop but I don't want it to catch previous matches its made, e.g 240, 10. What would pattern or code what I have to implement? maybe turn it into an array of sorts? So if the sentence was

"I had 290 Cats and I lost 50 and now I have 240,
I lost 230 Dogs and I now have 10."

But if I leave it looping, I don't want it to keep giving me 10 or 240 and only give me a match when the following sentence is added..

"I purchased 153 Lizards and some ran away so now I have 125"

then it should match the number 125 and then keep looping until another number after the word "have" appears.

at the end I meant to say until another sentence with a number after the word "have" appears.

Share this post


Link to post
Share on other sites

I am addressing your query from posts #11 & #12.

Local $iLoopNum = 0, $iPrev_Ubound = 0
Local $sString = "I had 290 Cats and I lost 50 now I have 240"

Do
    $iLoopNum += 1

    $aResults = StringRegExp($sString, "(?i)have\h*(\d+)", 3)
    If UBound($aResults) <> $iPrev_Ubound Then
        $iPrev_Ubound = UBound($aResults)
        MsgBox(0, "Results", 'On loop: ' & $iLoopNum & @CRLF & 'The number after the latest, last "have" is ' & $aResults[UBound($aResults) - 1])
    EndIf

    ; Add a line to test string
    If $iLoopNum = 4 Then $sString &= @CRLF & "I lost 230 Dogs and I now have 10 "
    If $iLoopNum = 12 Then $sString &= @CRLF & "I purchased 153 Lizards and some ran away so now I have 125"
Until $iLoopNum = 20

 

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

1 hour ago, Malkey said:

I am addressing your query from posts #11 & #12.

Local $iLoopNum = 0, $iPrev_Ubound = 0
Local $sString = "I had 290 Cats and I lost 50 now I have 240"

Do
    $iLoopNum += 1

    $aResults = StringRegExp($sString, "(?i)have\h*(\d+)", 3)
    If UBound($aResults) <> $iPrev_Ubound Then
        $iPrev_Ubound = UBound($aResults)
        MsgBox(0, "Results", 'On loop: ' & $iLoopNum & @CRLF & 'The number after the latest, last "have" is ' & $aResults[UBound($aResults) - 1])
    EndIf

    ; Add a line to test string
    If $iLoopNum = 4 Then $sString &= @CRLF & "I lost 230 Dogs and I now have 10 "
    If $iLoopNum = 12 Then $sString &= @CRLF & "I purchased 153 Lizards and some ran away so now I have 125"
Until $iLoopNum = 20

 

if I wanted this to sleep until I added a new line into the notepad, what would be the Until condition for waiting until detecting a new line added and having the msgbox display the latest number ( I know you currently don't have it set to accessing a notepad but that's not necessary ).

Edited by Chromed

Share this post


Link to post
Share on other sites

This example uses Notepad.
Note the Reg Exp pattern ends with "[\.\s]" which represents a dot, a space, or a vertical white space ( a Line Feed, or Carriage Return). This must follow the digits that follow "have " in Notepad so that the digits match.

So when you enter a line in Notepad, ending with say "have 999" finish the line with either a dot, a space, or the Enter key.

Note: When the Note window is closed, the script ends.

Local $sFileName = "TestHaveNum.txt"
If FileExists($sFileName) Then FileDelete($sFileName)

FileWrite($sFileName, "I had 290 Cats and I lost 50 now I have 240" & @CRLF)

Local $iPrev_Ubound = 0
Run("Notepad.exe " & $sFileName)
$hWin = WinWaitActive($sFileName & " - Notepad")

Do
    $aResults = StringRegExp(ControlGetText($hWin, "", "Edit1"), "(?i)have\h*(\d+)[\.\s]", 3)
    If UBound($aResults) <> $iPrev_Ubound Then
        $iPrev_Ubound = UBound($aResults)
        MsgBox(0, "Results", 'The number after the latest, last "have" is ' & $aResults[UBound($aResults) - 1])
    EndIf
    Sleep(100)

    ; Copy and add these lines one at a time to notepad.
    ; I lost 230 Dogs and I now have 10
    ; I purchased 153 Lizards and some ran away so now I have 125
Until WinExists($hWin) <> 1

 

Share this post


Link to post
Share on other sites
11 hours ago, Malkey said:

This example uses Notepad.
Note the Reg Exp pattern ends with "[\.\s]" which represents a dot, a space, or a vertical white space ( a Line Feed, or Carriage Return). This must follow the digits that follow "have " in Notepad so that the digits match.

So when you enter a line in Notepad, ending with say "have 999" finish the line with either a dot, a space, or the Enter key.

Note: When the Note window is closed, the script ends.

Local $sFileName = "TestHaveNum.txt"
If FileExists($sFileName) Then FileDelete($sFileName)

FileWrite($sFileName, "I had 290 Cats and I lost 50 now I have 240" & @CRLF)

Local $iPrev_Ubound = 0
Run("Notepad.exe " & $sFileName)
$hWin = WinWaitActive($sFileName & " - Notepad")

Do
    $aResults = StringRegExp(ControlGetText($hWin, "", "Edit1"), "(?i)have\h*(\d+)[\.\s]", 3)
    If UBound($aResults) <> $iPrev_Ubound Then
        $iPrev_Ubound = UBound($aResults)
        MsgBox(0, "Results", 'The number after the latest, last "have" is ' & $aResults[UBound($aResults) - 1])
    EndIf
    Sleep(100)

    ; Copy and add these lines one at a time to notepad.
    ; I lost 230 Dogs and I now have 10
    ; I purchased 153 Lizards and some ran away so now I have 125
Until WinExists($hWin) <> 1

 

I ran the code and the msgbox never popped up, not even once I added a line. Did I miss something obvious or ?

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

It will report the digits that exist after 'have' or 'have and a series of vertical white spaces' when i push enter provided that there are no nonnumeric (or unhandled) characters in that string of numbers after 'have' ,  without a vertical white space between them

if you jack up that first sentence by deleting 240 or adding any nonnumeric characters without a space, explosions (im guessing 0 -1 is no good). 

 

Edited by iamtheky

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

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