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 

RegEx works simple case, but not real life

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



Joined: 26 Jun 2007
Posts: 52

PostPosted: Tue Oct 13, 2009 11:37 am    Post subject: RegEx works simple case, but not real life Reply with quote

I need to match anything that starts with "[[Daten:" and ends with "]]"
Regex that works:

Code:
 \[\[\Datei:.*.*?]]


It works on
Code:
useful info[[Datei:string to match]]useful info


but not on
Code:

useful info[[Datei:string
something here on a new line
to match]]useful info[[something that don't need to be matched]] something else


And i could not figure out way to fix it...
Back to top
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Tue Oct 13, 2009 12:29 pm    Post subject: Reply with quote

Code:
haystack =
(
useful info[[Datei:string
something here on a new line
to match]]useful info[[something that don't need to be matched]] something else
)

needle = m)\[\[Datei:(.*?)]]

RegExMatch(haystack, needle, result)

MsgBox % result1


Returns:

Quote:
string
something here on a new line
to match


m) = multi-line (optional), in this case it is not necessary. See also the x) option
(.*?) = return everything between the parentheses

HTH
Back to top
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Tue Oct 13, 2009 12:32 pm    Post subject: Reply with quote

. (dot) in AutoHotkey Help Regular Expressions (RegEx) - Quick Reference wrote:
A dot matches any single character (except newline: `r and `n). For example, ab. matches abc and abz and ab_

Code:
variable1=useful info[[Datei:string to match]]useful info
variable2=
(Join`r`n
useful info[[Datei:string
something here on a new line
to match]]useful info[[something that don't need to be matched]] something else
)
Loop 2
   MsgBox % RegExMatch(variable%A_Index%,"\[\[Datei:[^]]*?]]")

;So this is not enough to work on `r`n which is included when you use FileRead or Notepad Edit text etc.
MsgBox % RegExMatch(variable2, "m)\[\[Datei:(.*?)]]")

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
flak



Joined: 02 Oct 2009
Posts: 283

PostPosted: Tue Oct 13, 2009 1:29 pm    Post subject: Reply with quote

. (dot) in AutoHotkey Help Regular Expressions (RegEx) - Quick Reference wrote:
A dot matches any single character (except newline: `r and `n). For example, ab. matches abc and abz and ab_

s option wrote:
DotAll. This causes a period (.) to match all characters including newlines (normally, it does not match newlines).

RegExMatch(variable2, "s)\[\[Datei:(.*?)]]")
No?
Back to top
View user's profile Send private message
TestPilot



Joined: 26 Jun 2007
Posts: 52

PostPosted: Tue Oct 13, 2009 1:52 pm    Post subject: Reply with quote

Thanks n-l-i-d, HotKeyIt and flak! It worked somehow
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