AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I need help with RegExMatch
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TalksWithComputers



Joined: 16 Aug 2009
Posts: 16

PostPosted: Wed Nov 11, 2009 9:54 pm    Post subject: I need help with RegExMatch Reply with quote

We get order confirmations from our online store by e-mail. I would like to extract text from the e-mail for use in a different program. It would save us tons of time!

I have read a lot about RegExMatch, which has the ability to extract a substring from the clipboard. My programming skills are too limited to see through it. I simply can't figure out how to set the search parameters right. And I don't know what it means for me when I have stored the substring in an array. how do I access this info later?

Here's the structure of the input:
Quote:
Ihre Informationen
Rechungsadresse
Firma:
Name: Jane Doe
Adresse : Some road 123
...


Here is what I have come up with so far:
Code:
F7::
send ^a

clipboard = 
Send ^c
ClipWait 
RegExMatch (Clipboard, "m)Name: *Adresse:", Kundenname)

IfWinNotExist, ahk_class Notepad
   Run, notepad.exe
WinWait Editor
WinActivate Editor

send %kundenname1%

Return



Result: The script executes and switches to notepad but nothing happens there. I guess I haven't gotten the needle haystack part right.

The Notepad part is just for testing purposes.
I need to extract many more sub strings like the address, article number and so forth. But one thing at a time...

Any suggestions on how to proceed short of spending a dozen hours getting to know regexmatch?

thanks a lot!
Back to top
View user's profile Send private message
Guest






PostPosted: Wed Nov 11, 2009 10:22 pm    Post subject: Reply with quote

I might start here:
Code:
RegExMatch(Clipboard, "m)Name: *Adresse:", Kundenname) ; no space
Back to top
TalksWithComputers



Joined: 16 Aug 2009
Posts: 16

PostPosted: Wed Nov 11, 2009 10:35 pm    Post subject: Reply with quote

There's no error message so I don't think there's a syntax error. Deleting the space changes nothing. Script runs, nothing happens.
Back to top
View user's profile Send private message
MasterFocus



Joined: 08 Apr 2009
Posts: 882
Location: Rio de Janeiro - RJ - Brasil

PostPosted: Thu Nov 12, 2009 1:58 am    Post subject: Reply with quote

"*" is not a wildcard in RegEx
Correct is ".*"

>> http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm
_________________
Antonio França
aka MasterFocus aka Tunis
+ My AHK stuff: ~MasterFocus
+ AHK @ irc.freenode.net: #ahk
Contact: PM only !
Back to top
View user's profile Send private message
rocknrollgolfer



Joined: 03 Oct 2009
Posts: 8

PostPosted: Thu Nov 12, 2009 5:22 am    Post subject: Reply with quote

This is a great site for testing regex expressions:

http://www.robagar.com/liveregex/
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Thu Nov 12, 2009 5:24 am    Post subject: Reply with quote

I don't think using the multiline option is going to work with your code in this instance, however the dot-all option should:


Code:
var=
(
Ihre Informationen
Rechungsadresse
Firma:
Name: Jane Doe
Adresse : Some road 123
)

RegExMatch(var,"s)Name: (?P<Name>\V+).*?Adresse : (?P<Adresse>\V+)",Kunden) ; \V matches any char that is not vertical whitespace (linefeed, return, etc.)
MsgBox % "Matching Name: " KundenName "`n"
  . "Matching Adresse: " KundenAdresse
return

_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message
nick



Joined: 24 Aug 2005
Posts: 499
Location: Berlin / Germany

PostPosted: Thu Nov 12, 2009 7:32 am    Post subject: Reply with quote

sinkface wrote:
\V matches any char that is not vertical whitespace (linefeed, return, etc.)

Nice one! Wink So we have some kind of multiline substitution:
Code:
#NoEnv
Var=
(Join`r`n
Ihre Informationen
Rechungsadresse
Firma:
Name: Jane Doe
Adresse: Some road 123
)
RegExMatch(Var,"\RName:\s+(?P<Name>\V+)\RAdresse:\s+(?P<Adresse>\V+)",Kunden) ; \V matches any char that is not vertical whitespace (linefeed, return, etc.)
MsgBox % "Matching Name: " KundenName "`n"
       . "Matching Adresse: " KundenAdresse
ExitApp

_________________
nick

denick @ http://de.autohotkey.com/forum/
Back to top
View user's profile Send private message
TalksWithComputers



Joined: 16 Aug 2009
Posts: 16

PostPosted: Thu Nov 12, 2009 8:22 pm    Post subject: Reply with quote

Everybody,

Thank you! Looks quite impressive. This gives me a very good starting point!

Just a question: How do I incorporate this Code into my script that is supposed to access the clipboard and not some predefined variable?

specifically: How do I apply this piece of code

Code:
Join`r`n


To the clipboard?
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Nov 12, 2009 9:09 pm    Post subject: Reply with quote

Read up on Clipboard, ClipboardAll, and ClipWait.

http://www.autohotkey.com/docs/misc/Clipboard.htm

To use it you might do something like the following.
Code:

;clear clipboard
clipboard =

Winactivate, Notepad
WinWait, Notepad

;Select all & Copy
send ^a^c

clipwait, 2
if ErrorLevel {
    msgbox, Failed to copy clipbaord
    return
}

var := clipboard  ;copy clipboard information

Back to top
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Thu Nov 12, 2009 9:12 pm    Post subject: Reply with quote

(Previous was me, seems i logged out somehow Embarassed )

As for the join, it looks like you have it right.
Back to top
View user's profile Send private message Yahoo Messenger
TalksWithComputers



Joined: 16 Aug 2009
Posts: 16

PostPosted: Thu Nov 12, 2009 9:52 pm    Post subject: Reply with quote

Just for clarification: What I want to do is this:
Whenever an order arrives, I want to press a hotkey that copies the mail, extracts the texts and pastes the contents to a different program.

Ok, I've figured it out:
Code:
#NoEnv
F7::
clipboard = 
send ^a^c
ClipWait 

RegExMatch(Clipboard,"s)Firma: (?P<Firma>\V+).*?Name: (?P<Name>\V+).*?Adresse : (?P<Adresse>\V+).*?Stadt : (?P<Stadt>\V+)",Kunden) ; \V matches any char that is not vertical whitespace (linefeed, return, etc.)

MsgBox % "Matching Firma: " KundenFirma "`n"
  . "Matching Name: " KundenName "`n"
  . "Matching Adresse: " KundenAdresse "`n"
  . "Matching Stadt: " KundenStadt
Return

As you can see, I've even expanded it a little. So great, thanks again!

Question
What do I do if the same matches appear later in the clipboard again because of the delivery address? Address, Name etc?
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Fri Nov 13, 2009 4:04 am    Post subject: Reply with quote

TalksWithComputers wrote:
Question
What do I do if the same matches appear later in the clipboard again because of the delivery address? Address, Name etc?


I'm not sure I understand your question, could you explain a little further?
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Fri Nov 13, 2009 4:38 am    Post subject: Reply with quote

sinkfaze wrote:
TalksWithComputers wrote:
Question
What do I do if the same matches appear later in the clipboard again because of the delivery address? Address, Name etc?


I'm not sure I understand your question, could you explain a little further?


I believe he's saying something like:

Name: Yoda
Address: 123 something..

Name: Anakin
Address: Deathstar

Ect.

I am not sure, it may only take the first one and not the second. The documentation doesn't comment about that specific scenario. I will check momentarily.

EDIT:
From what i can tell, the 'matched variable' is the base, and each named variable is added onto that. I managed to have two in the same variable, but only got the first match. In order to get the second one, you'd have to change the starting position offset equal or after the ending of the last match, probably using a loop.

Code:

;when the code is paused, look under the variables to see what i am talking about.
txt =
(
Name: Yoda
Address: 123 something..

Name: Anakin
Address: Deathstar
)

pos := RegExMatch(txt,"si)Name: (?P<Name>\V+).*?Address: (?P<Address>\V+)", match)
pause
Back to top
View user's profile Send private message Yahoo Messenger
TalksWithComputers



Joined: 16 Aug 2009
Posts: 16

PostPosted: Fri Nov 13, 2009 8:14 am    Post subject: Reply with quote

Here's what I meant:
Quote:
Billing Address
Firma:
Name: Jane Doe
Adresse : Some road 123
City :
...

Delivery Address
Firma: Acme inc
Name: Jane Doe
Adresse : Some other road 135
City :


Would it be possible to split the clipboard at "delivery address" into 2
variables for which separate Regexmatches could be done? This way one could omit the problem of having 2 identically named elements.

Stringsplit looks kind of promising. Any thoughts on that one?
Back to top
View user's profile Send private message
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Fri Nov 13, 2009 8:29 am    Post subject: Reply with quote

TalksWithComputers wrote:
Here's what I meant:
Quote:
Billing Address
Firma:
Name: Jane Doe
Adresse : Some road 123
City :
...

Delivery Address
Firma: Acme inc
Name: Jane Doe
Adresse : Some other road 135
City :


Would it be possible to split the clipboard at "delivery address" into 2
variables for which separate Regexmatches could be done? This way one could omit the problem of having 2 identically named elements.

Stringsplit looks kind of promising. Any thoughts on that one?


StringSplit appears to split according to individual characters, so you can split with `r or `n, or `t or any of those three at once, but not a combination in a row. To do that you'd have to find the combination and convert it to a unused combination in your text. (Like Tilde ~ perhaps?)

If you do StringSplit, it might look something like this.
Code:

;note, stringreplace based on FIXED KNOWN patterns between your data.
;if you cannot guarantee the pattern, i don't recommend this.
StringReplace, text, clipboard, `r`n`r`n, ~, All
StringSplit, text, text, ~

;Text0 holds the number of matches found, so it's values would be Text1-TextN, where N is the number expressed in Text0
loop, %text0%{
  ;your code here with regex
  ;dealing with your haystack as text%a_index%
}



You can also loop like i mentioned before.
Code:

;starting position is 1. If there's a better way to do this,
;i'm interested in knowing, but this should work.
pos :=1
loop, {
;pos should point to starting of regex match now.
;match holds the contents of interest.
  pos := regexmatch(haystack, needle, match, pos)

;no more patterns.
  if pos = 0
    break

;regex on 'match' and handle data.

;update starting location for next match.
  pos += strlen(match)
}

Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group