RegExMatch Multiline get line number it was found on

Get help with using AutoHotkey and its commands and hotkeys
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

RegExMatch Multiline get line number it was found on

09 Oct 2014, 14:37

Is it possible for RegExMatch to return the line number the match was found on in a multiline variable?
In the example below "there" is found on the 2nd line

Code: Select all

var =
(
                 The Egyptian pyramids are ancient pyramid-shaped 

   There are 138 pyramids discovered in Egypt as of 2008.[1][2] 

      The earliest known Egyptian pyramids are found at Saqqara,

  
  
 

          The most famous Egyptian pyramids are those found at Giza
  
   By the time of the early dynastic period of Egyptian history

  The second historically documented Egyptian pyramid is attributed   
)
Var := RegExReplace(RegExReplace(Var,"`am)^\s+"),"\s*$") ;removes blank lines and whitespace before and after each line in  multiline variable

var2 := RegExMatch(var,"`aim)^there",outputvar)
MsgBox %var2%`n%outputvar%
exitapp
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: RegExMatch Multiline get line number it was found on

09 Oct 2014, 15:05

One way would be to remove everything after the first occurrence of the search term, then count the linefeeds that remain.
Instead of using the inner RegExReplace, you could use InStr to find the position of the search term, and SubStr to get all of the text preceding the search term.

Code: Select all

var =
(
                 The Egyptian pyramids are ancient pyramid-shaped

   There are 138 pyramids discovered in Egypt as of 2008.[1][2]

      The earliest known Egyptian pyramids are found at Saqqara,

 
 
 

          The most famous Egyptian pyramids are those found at Giza
 
   By the time of the early dynastic period of Egyptian history

  The second historically documented Egyptian pyramid is attributed   
)
Search := "Giza"
RegExReplace(RegExReplace(var, "s).*?" Search "\K.+"), "\R",, n)
MsgBox, % n + 1
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: RegExMatch Multiline get line number it was found on

09 Oct 2014, 15:49

Thanks Kon, that works perfectly.

When I get home tonight I will study it to see how it is doing it.

thanks again.
DataLife
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
AlphaBravo
Posts: 476
Joined: 29 Sep 2013, 22:59

Re: RegExMatch Multiline get line number it was found on

09 Oct 2014, 22:50

@kon, it will return the total text lines if search word is not found ;-)

Code: Select all

Search := "Giza"
RegExReplace(RegExReplace(var, "s)" Search ".*",,foundit), "\R",,n)
MsgBox % foundit ? n+1 : 0
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: RegExMatch Multiline get line number it was found on

09 Oct 2014, 22:58

kon wrote:One way would be to remove everything after the first occurrence of the search term, then count the linefeeds that remain.
Instead of using the inner RegExReplace, you could use InStr to find the position of the search term, and SubStr to get all of the text preceding the search term.

Code: Select all

var =
(
                 The Egyptian pyramids are ancient pyramid-shaped

   There are 138 pyramids discovered in Egypt as of 2008.[1][2]

      The earliest known Egyptian pyramids are found at Saqqara,

 
 
 

          The most famous Egyptian pyramids are those found at Giza
 
   By the time of the early dynastic period of Egyptian history

  The second historically documented Egyptian pyramid is attributed   
)
Search := "Giza"
RegExReplace(RegExReplace(var, "s).*?" Search "\K.+"), "\R",, n)
MsgBox, % n + 1
I found a problem with this. If the string is not found it always returns the number of the last line.
So I fixed it using InStr.

Code: Select all

var =
(
                 The Egyptian pyramids are ancient pyramid-shaped

   There are 138 pyramids discovered in Egypt as of 2008.[1][2]

      The earliest known Egyptian pyramids are found at Saqqara,

 
 
 

          The most famous Egyptian pyramids are those found at Giza
 
   By the time of the early dynastic period of Egyptian history

  The second historically documented Egyptian pyramid is attributed   
  emoves blank lines and whitespace before and after each line in  multiline variable
)
Search := "z"
var := RegExReplace(RegExReplace(var,"`am)^\s+"),"\s*$") ;removes blank lines and whitespace before and after each line in  multiline variable
FoundIt := InStr(Var,Search)
if FoundIt = 0
 MsgBox Not in string
else
 {
  RegExReplace(RegExReplace(var, "is).*?" Search "\K.+"), "\R",, n)
  MsgBox, % n + 1
 }
ExitApp
Can your RegEx be modified to report 0 if the string is not found?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
AlphaBravo
Posts: 476
Joined: 29 Sep 2013, 22:59

Re: RegExMatch Multiline get line number it was found on

09 Oct 2014, 23:07

@ DataLife, Hello ?? did you read my post above
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: RegExMatch Multiline get line number it was found on

10 Oct 2014, 00:06

I've learned something new.

Code: Select all

var =
(
                 The Egyptian pyramids are ancient pyramid-shaped

   There are 138 pyramids discovered in Egypt as of 2008.[1][2]

      The earliest known Egyptian pyramids are found at Saqqara,

 
 
 

          The most famous Egyptian pyramids are those found at Giza
 
   By the time of the early dynastic period of Egyptian history

  The second historically documented Egyptian pyramid is attributed   
  emoves blank lines and whitespace before and after each line in  multiline variable
)
Search := "gia"
;Search := "giza"
;var := RegExReplace(RegExReplace(var,"`am)^\s+"),"\s*$") ;removes blank lines and whitespace before and after each line in  multiline variable
var := RegExReplace(var,"`am)^\s+|[ \t]+$")
if n := (var~="i)" Search)
   RegExReplace(RegExReplace(var, "is).*?" Search "\K.+"), "\R",,n) n+=1
MsgBox % n
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: RegExMatch Multiline get line number it was found on

10 Oct 2014, 02:33

AlphaBravo wrote:@kon, it will return the total text lines if search word is not found ;-)

Code: Select all

Search := "Giza"
RegExReplace(RegExReplace(var, "s)" Search ".*",,foundit), "\R",,n)
MsgBox % foundit ? n+1 : 0
I missed this by AlphaBravo, this works great.

thank you very much AlphaBravo and Kon
DataLife
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.

Return to “Ask For Help”

Who is online

Users browsing this forum: afe, ahk7, AHKStudent, c7aesa7r, coris, EEEEE, Google [Bot], tuineen and 155 guests