AutoHotkey Community

It is currently May 26th, 2012, 11:53 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: November 11th, 2009, 10:54 pm 
Offline

Joined: August 16th, 2009, 2:02 pm
Posts: 20
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 11th, 2009, 11:22 pm 
I might start here:
Code:
RegExMatch(Clipboard, "m)Name: *Adresse:", Kundenname) ; no space


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 11th, 2009, 11:35 pm 
Offline

Joined: August 16th, 2009, 2:02 pm
Posts: 20
There's no error message so I don't think there's a syntax error. Deleting the space changes nothing. Script runs, nothing happens.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 2:58 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
"*" is not a wildcard in RegEx
Correct is ".*"

>> http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 6:22 am 
Offline

Joined: October 3rd, 2009, 2:23 pm
Posts: 8
This is a great site for testing regex expressions:

http://www.robagar.com/liveregex/


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 6:24 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 8:32 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
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 :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 9:22 pm 
Offline

Joined: August 16th, 2009, 2:02 pm
Posts: 20
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 10:09 pm 
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



Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 10:12 pm 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
(Previous was me, seems i logged out somehow :oops: )

As for the join, it looks like you have it right.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 12th, 2009, 10:52 pm 
Offline

Joined: August 16th, 2009, 2:02 pm
Posts: 20
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2009, 5:04 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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?

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2009, 5:38 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2009, 9:14 am 
Offline

Joined: August 16th, 2009, 2:02 pm
Posts: 20
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 13th, 2009, 9:29 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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)
}



Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 32 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], hyper_, Leef_me, patgenn123, Pulover, RaptorX, rbrtryn, tidbit, XstatyK and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group