Jump to content
Sign in to follow this  
leo11173

Keeping the last match of a StringRegExp

Recommended Posts

leo11173

Hi, i am trying to transfert lines monitored from a chat to another chat using StringRegExp but i am kinda lost and did not manage to search what i was looking for.

The main problem is that i would like to only catch the last line said from the chat by user "themessager". Actualy the code will report all the lines themessager publiched on the chat each time i execute the script.

$Text=ControlGetText("[CLASS:ThunderRT6FormDC]","","[CLASSNN:RichTextWndClass4]")
$Value=StringSplit($Text,@CRLF)
For $i=1 To $Value[0]
    If StringRegExp($Value[$i],"themessager") Then
controlSend("[CLASS:ThunderRT6FormDC]", "", "Edit1", $Value[$i])   
        
    EndIf
Next

Thanks in advance.

Share this post


Link to post
Share on other sites
sahsanu

The main problem is that i would like to only catch the last line said from the chat by user "themessager". Actualy the code will report all the lines themessager publiched on the chat each time i execute the script.

Try starting from the end and if it matches the user name then exit loop. Take this as an example:

$Text="[jim] Hi boy" & @CRLF & _
"[themessager] Hi" & @CRLF & _
"[themessager] How are you doing today?" & @CRLF & _
"[jim] I'm doing fine" & @CRLF & _
"[themessager] this is my last line" & @CRLF & _
"[jim] ok"

$Value=StringSplit($Text,@CRLF)

For $i=$Value[0] To 1 Step -1
    If StringRegExp($Value[$i],"themessager") Then
    ;controlSend("[CLASS:ThunderRT6FormDC]", "", "Edit1", $Value[$i])
        ConsoleWrite($Value[$i] & @LF)
        ExitLoop
    EndIf
Next

Cheers,

sahsanu

Share this post


Link to post
Share on other sites
leo11173

Thanks sahsanu you solved my problem.

here's the correct script now :

It will get text of specified user (exemple user is named themessager there) from chat window then send the last line user posted to another window

$Text=ControlGetText("[CLASS:ThunderRT6FormDC]","","[CLASSNN:RichTextWndClass5]")
$Value=StringSplit($Text,@CRLF)
For $i=$Value[0] To 1 Step -1
 If StringRegExp($Value[$i],"themessager") Then
     ;Envoi des dialogues message dans le chat
controlSend("[CLASS:ThunderRT6FormDC]", "", "Edit1", $Value[$i] & @CRLF)   
        
        ExitLoop
    EndIf
Next

Share this post


Link to post
Share on other sites
jchd

There is much a simpler way using a single regexp (without duplicating then splitting data):

Local $s =  "abc hoigzho fhoijg def" & @CRLF & _
            "fgh gzjpogerjkpo" & @CRLF & _
            "abc jogrjpher xyz jfgzjo" & @CRLF & _
            "def jiogrjohjpoh" & @CRLF & _
            "uvw jpgejpher def jiofgd" & @CRLF & _
            "def kopgrekpo" & @CRLF & _
            "xyz jipgrejphe" & @CRLF

Local $user = "def"
Local $text = StringRegExp($s, "(*CRLF)(?ms)^" & $user & "([^\n]*)$(?!.*^" & $user & ")", 1)
_ArrayDisplay($text, $user & " last said")

If you want to keep user name at front of it's phrase, remove the perenthesis and use

Local $text = StringRegExp($s, "(*CRLF)(?ms)^" & $user & "[^\n]*$(?!.*^" & $user & ")", 1)


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)

Share this post


Link to post
Share on other sites
leo11173

@jchd

I did not manage to execute your code exemple.

Share this post


Link to post
Share on other sites
Juvigy

Put this in the beginning :

#Include <Array.au3>

Share this post


Link to post
Share on other sites
jchd

Sorry I forgot to mention the include.

Many common includes get in automagically in my script testing template.


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)

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  

×