find #Include line by regex

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

find #Include line by regex

19 Sep 2018, 05:46

I would like to find the include line in a file and extract it's Information.
I have the following needle, but the named subpattern Path contains the comment.
How can this needle be improved?

Code: Select all

NeedleInclude = O)^\s*#Include\s+(\*i )?(?P<Path>.*)(\s+;(?P<Comment>.*))?
Here a small example ( I had to alter the #Include to #Incl ):

Code: Select all

String =
(
test
#Incl C:\FileB ;comment
test
test
#Incl   *i C:\File C   ;comment
test
test
#Incl   C:\Path A\File D.ahk
)

NeedleInclude = O)^\s*#Incl\s+(\*i )?(?P<Path>.*)(\s+;(?P<Comment>.*))?
Loop, parse, String, `n, `r
  {
    If RegExMatch(A_LoopField, NeedleInclude, Match){
      Found .= A_Index " " Match.Path "`n"
    }
  }
MsgBox %Found%
ciao
toralf
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: find #Include line by regex

19 Sep 2018, 05:55

One solution that eliminates the comments is this

Code: Select all

NeedleInclude = O)^\s*#Incl\s+(\*i )?(?P<Path>[^;]*)(\s+;(?P<Comment>.*))?
But it doesn't find the comments now...
ciao
toralf
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: find #Include line by regex

19 Sep 2018, 12:14

1. O)^\s*#Incl\s+(\*i )?(?P<Path>.*)(\s+;(?P<Comment>.*))? - .* in path has matched everything until the end of line already, comment is marked optional and there arent any more tokens for it to match, so it fails
2. O)^\s*#Incl\s+(\*i )?(?P<Path>[^;]*)(\s+;(?P<Comment>.*))? - [^;]* has matched everything up to the ; including the whitespace, \s+ then fails
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: find #Include line by regex

19 Sep 2018, 13:04

add lazy operator and anchor at the end O)^\s*#Incl\s+(\*i )?(?P<Path>.*?(\s+;(?P<Comment>.*))?$

Code: Select all

String =
(
test
#Incl C:\FileB ; comment1
test
test
#Incl   *i C:\File C   ; comment2
test
test
#Incl   C:\Path A\File D.ahk
)

NeedleInclude = O)^\s*#Incl\s+(\*i )?(?P<Path>.*?)(\s+;(?P<Comment>.*))?$

Loop, parse, String, `n, `r
  {
    If RegExMatch(A_LoopField, NeedleInclude, Match){
      Found .= A_Index " " Match.Path "`t `; comment = " Match.Comment "`n"
    }
  }
MsgBox %Found%
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: find #Include line by regex

19 Sep 2018, 16:30

Thanks a lot
ciao
toralf

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1, MrDoge and 247 guests