 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
TalksWithComputers
Joined: 16 Aug 2009 Posts: 16
|
Posted: Wed Nov 11, 2009 9:54 pm Post subject: I need help with RegExMatch |
|
|
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 |
|
 |
Guest
|
Posted: Wed Nov 11, 2009 10:22 pm Post subject: |
|
|
I might start here:
| Code: | | RegExMatch(Clipboard, "m)Name: *Adresse:", Kundenname) ; no space |
|
|
| Back to top |
|
 |
TalksWithComputers
Joined: 16 Aug 2009 Posts: 16
|
Posted: Wed Nov 11, 2009 10:35 pm Post subject: |
|
|
| 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 |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 882 Location: Rio de Janeiro - RJ - Brasil
|
|
| Back to top |
|
 |
rocknrollgolfer
Joined: 03 Oct 2009 Posts: 8
|
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
Posted: Thu Nov 12, 2009 5:24 am Post subject: |
|
|
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 |
|
 |
nick
Joined: 24 Aug 2005 Posts: 499 Location: Berlin / Germany
|
Posted: Thu Nov 12, 2009 7:32 am Post subject: |
|
|
| sinkface wrote: | | \V matches any char that is not vertical whitespace (linefeed, return, etc.) |
Nice one! 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 |
|
 |
TalksWithComputers
Joined: 16 Aug 2009 Posts: 16
|
Posted: Thu Nov 12, 2009 8:22 pm Post subject: |
|
|
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
To the clipboard? |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Nov 12, 2009 9:09 pm Post subject: |
|
|
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
|
Posted: Thu Nov 12, 2009 9:12 pm Post subject: |
|
|
(Previous was me, seems i logged out somehow )
As for the join, it looks like you have it right. |
|
| Back to top |
|
 |
TalksWithComputers
Joined: 16 Aug 2009 Posts: 16
|
Posted: Thu Nov 12, 2009 9:52 pm Post subject: |
|
|
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 |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
Posted: Fri Nov 13, 2009 4:04 am Post subject: |
|
|
| 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 |
|
 |
rtcvb32
Joined: 17 Feb 2008 Posts: 125
|
Posted: Fri Nov 13, 2009 4:38 am Post subject: |
|
|
| 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 |
|
 |
TalksWithComputers
Joined: 16 Aug 2009 Posts: 16
|
Posted: Fri Nov 13, 2009 8:14 am Post subject: |
|
|
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 |
|
 |
rtcvb32
Joined: 17 Feb 2008 Posts: 125
|
Posted: Fri Nov 13, 2009 8:29 am Post subject: |
|
|
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|