Jump to content
Sign in to follow this  
d0n

Stringregexp

Recommended Posts

d0n

what i am trying to do is to get info from the strings

but i ran into some problems

1) i am not sure how to use the "|" function in StringRegExp to get both "hit" and "slash"

2) The name on the 3rd line changes from "You" to "A Tall Dragon", not sure how to have it search for both names

Global $i = 1, $name, $dmg

While 1
    $read = FileReadLine(@scriptdir&"\test.txt", $i)
    If @error = -1 Then ExitLoop
    $dmg= StringRegExp($read, "for (\d+)", 1)
    $name = StringRegExp($read, "(\b\w+\b) hit", 1)
    If $dmg = 0 Then
    Else
    $dmg1 = $dmg[0]
    $name1 = $name[0]
    MsgBox("", "Info", $name1 &" = " & $dmg1)   
    EndIf
    $i = $i + 1
Wend

The txt file

You slash Dragon for 1183 damage.
You hit Dragon for 1217 damage.
A Tall Dragon hits you for 96 damage.

Share this post


Link to post
Share on other sites
Authenticity

Dim $sStr = 'You slash Dragon for 1183 damage.' & @CRLF & _
            'You hit Dragon for 1217 damage.' & @CRLF & _
            'A Tall Dragon hits you for 96 damage.'

Dim $sPatt = '(.*?)(?:hit|slashe?)s?.*?for\s*(\d+)'
Dim $aLines = StringSplit($sStr, @CRLF, 1)

For $i = 1 To $aLines[0]
    Local $aMatch = StringRegExp($aLines[$i], $sPatt, 1)
    If UBound($aMatch) = 2 Then ConsoleWrite('Hit: ' & $aMatch[0] & @TAB & 'Dmg: ' & $aMatch[1] & @CRLF)
Next

Share this post


Link to post
Share on other sites
d0n

hi one more question,

there happens to be a timestamp in front of each line [1:41:21 PM Jun 04 2009]

How come this doesn't work?

Dim $sPatt = '(]) (.*?)(?:hit|slashe?)s?.*?for\s*(\d+)'

Share this post


Link to post
Share on other sites
Authenticity

Try using (?i) in the beginning if the pattern because it won't match, right now, HiTs or SlASh or foR, for example:

Dim $sStr = '[1:41:21 PM Jun 04 2009] You slash Dragon for 1183 damage.'
Dim $sPatt = '(?i)\[[^]]*\]\s*(.*?)(?:hit|slashe?)s?.*?for\s*(\d+)'
Dim $aMatch

$aMatch = StringRegExp($sStr, $sPatt, 1)
If UBound($aMatch) = 2 Then ConsoleWrite('Hit: ' & $aMatch[0] & @TAB & 'Dmg: ' & $aMatch[1] & @CRLF)

Share this post


Link to post
Share on other sites
d0n

thank you for the help, the pattern part is really confusing lol

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.