 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Valentina Guest
|
Posted: Thu Feb 11, 2010 4:46 am Post subject: How to extract Patterns from a Textfile? |
|
|
Hi all,
I have a problem I just can not get solved.
Say I have a text like this:
My {dog|cat} is a pet, which is not {good|great|fantastic}. I always need to {feed it|give it food}. It drives {me|us|my family} {crazy|insane} {and|and also} eats {my|our} {newspaper|papers}.
I need to extract all strings that are in {...} to a seperate file.
So for this example I would get a file like this:
{dog|cat}
{good|great|fantastic}
{feed it|give it food}
{me|us|my family}
{crazy|insane}
{and|and also}
{my|our}
{newspaper|papers}
Has somebody an idea how I can achieve that?
This would help me a lot
Thank you very much! |
|
| Back to top |
|
 |
System Monitor
Joined: 09 Mar 2007 Posts: 509 Location: Unknown
|
|
| Back to top |
|
 |
adely Guest
|
Posted: Thu Feb 11, 2010 5:33 am Post subject: |
|
|
There is a grep function that you can use.
| Code: | text = My {dog|cat} is a pet, which is not {good|great|fantastic}. I always need to {feed it|give it food}. It drives {me|us|my family} {crazy|insane} {and|and also} eats {my|our} {newspaper|papers}.
grep(text, "({.*?})", matches,1,0,"`n")
msgbox, %matches%
; grep function: http://www.autohotkey.com/forum/topic16164.html
grep(h, n, ByRef v, s = 1, e = 0, d = "") {
v =
StringReplace, h, h, %d%, , All
Loop
If s := RegExMatch(h, n, c, s)
p .= d . s, s += StrLen(c), v .= d . (e ? c%e% : c)
Else Return, SubStr(p, 2), v := SubStr(v, 2)
}
|
|
|
| Back to top |
|
 |
ton80
Joined: 18 Dec 2009 Posts: 62
|
Posted: Thu Feb 11, 2010 5:53 am Post subject: |
|
|
im sure there is a better way, maybe one of the pros will chime in, but here is my attempt..
| Code: | test = My {dog|cat} is a pet, which is not {good|great|fantastic}. I always need to {feed it|give it food}. It drives {me|us|my family} {crazy|insane} {and|and also} eats {my|our} {newspaper|papers}.
Loop, Parse, test, }
{
newtest = %A_LoopField%`}
RegExMatch(newtest, "\{.+?\}", output)
msgbox, %output%
}
|
|
|
| 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
|