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 

How to extract Patterns from a Textfile?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Valentina
Guest





PostPosted: Thu Feb 11, 2010 4:46 am    Post subject: How to extract Patterns from a Textfile? Reply with quote

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 Smile

Thank you very much!
Back to top
System Monitor



Joined: 09 Mar 2007
Posts: 509
Location: Unknown

PostPosted: Thu Feb 11, 2010 4:53 am    Post subject: Reply with quote

There are many different ways to approach this. You could use any of the following commands.
http://www.autohotkey.com/docs/commands/RegExMatch.htm
http://www.autohotkey.com/docs/commands/LoopParse.htm
http://www.autohotkey.com/docs/commands/StringLeft.htm
http://www.autohotkey.com/docs/commands/StringSplit.htm
http://www.autohotkey.com/docs/commands/StringTrimLeft.htm
Back to top
View user's profile Send private message Visit poster's website
adely
Guest





PostPosted: Thu Feb 11, 2010 5:33 am    Post subject: Reply with quote

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

PostPosted: Thu Feb 11, 2010 5:53 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
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