Catching spaces with Regex (simple)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Benny-D
Posts: 271
Joined: 12 Mar 2014, 10:09

Catching spaces with Regex (simple)

18 Sep 2014, 05:45

Why is the number of spaces in the resulting message box in the script below is always 3?
(I did use the ^ to get the matching tied to the beginning of the line):

Code: Select all

Contents=
(
* Furnuture:
   * Tables:
      * Wooden tables
      * Metal tables
   * Chairs:
      * Plastic chairs
      * Wooden:
         * Broken wooden chairs
         * New wooden chairs
      * Metal chairs
   * Beds
)
Loop, parse, Contents, `n, `r
   {
   msgbox % """" . A_LoopField . """"
   FoundPos := RegExMatch(A_LoopField, "^(   )+\*", SubPat)   
   msgbox % """" . SubPat1 . """" 
   Loop, parse, SubPat1
      NumberOfSpaces := A_Index
   msgbox % "Number of spaces: " . NumberOfSpaces
   }
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Catching spaces with Regex (simple)

18 Sep 2014, 06:15

because ( ) has 3 spaces in them. try using FoundPos := RegExMatch(A_LoopField, "^(\s+)\*", SubPat)
User avatar
Benny-D
Posts: 271
Joined: 12 Mar 2014, 10:09

Re: Catching spaces with Regex (simple)

18 Sep 2014, 07:29

ahcahc wrote:because ( ) has 3 spaces in them
- How is that a reason ?! Did I not use ^ in the beginning and the \* in the end of the needle? And did I not use the + right after the 3-space-containing ( ) ? Does this all not have to match 3, or 6, or 9, or 12, etc. spaces?
ahcahc wrote:try using FoundPos := RegExMatch(A_LoopField, "^(\s+)\*", SubPat)
- Tried. It doesn't work (used %A_Space% now for better readability):

Code: Select all

Contents=
(
* Furnuture:
%A_Space%%A_Space%%A_Space%* Tables:
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Wooden
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Metal
%A_Space%%A_Space%%A_Space%* Chairs:
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Plastic
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Wooden
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Broken
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* New
%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%%A_Space%* Metal
)
Loop, parse, Contents, `n, `r
   {
   msgbox % """" . A_LoopField . """"
   FoundPos := RegExMatch(A_LoopField, "^(\s\s\s)+\*", SubPat)   
   msgbox % """" . SubPat1 . """" 
   Loop, parse, SubPat1
      NumberOfSpaces := A_Index
   msgbox % "Number of spaces: " . NumberOfSpaces
   }
lexikos
Posts: 6879
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Catching spaces with Regex (simple)

18 Sep 2014, 07:37

Loop, parse, SubPat1
NumberOfSpaces := A_Index
Why not use StrLen? :lol:
Why is the number of spaces in the resulting message box in the script below is always 3?
Because SubPat1 contains three spaces.

Why does SubPat1 contain three spaces? Your sub-pattern matches one or more times, but each time it matches, it captures exactly three spaces. The following should demonstrate this more clearly:

Code: Select all

RegExMatch("abc", "((\w)+)", m)
MsgBox,
(
subpattern containing repetitions: '%m1%'
repeated subpattern: '%m2%'
overall match: '%m%'
)
It's like this:

Code: Select all

Loop 3
   SubPat1 := "   "
No matter how many times you repeat it, SubPat1 will contain three spaces.
User avatar
Benny-D
Posts: 271
Joined: 12 Mar 2014, 10:09

Re: Catching spaces with Regex (simple)

18 Sep 2014, 08:10

lexikos wrote:
The following should demonstrate this more clearly:

Code: Select all

RegExMatch("abc", "((\w)+)", m)
MsgBox,
(
subpattern containing repetitions: '%m1%'
repeated subpattern: '%m2%'
overall match: '%m%'
)
- In your code I see you have a subpattern inside another subpattern! I have never seen this before and never knew this way was possible. The only thing that I knew so far was something like"(abc)de(fgh)ij(klm)", in which sub1 would be "abc", sub2 would be "fgh", sub3 would be "klm", and the overall pattern would be "abcdefghijklm". Can you firstly, please, explain to me the subpatterns naming policy in such cases like yours?

I can infer from your code that the broader subpattern (the one that contains another one inside) should be numbered first and then the smaller one, hence, we have m1 and m2 in your code, respectively. But what about such cases as, say, "((\w)+(sd)+)" ? In what order would subpatterns be numbered then?
User avatar
Benny-D
Posts: 271
Joined: 12 Mar 2014, 10:09

Re: Catching spaces with Regex (simple)

18 Sep 2014, 08:33

It just "dawned" on me that the essential reasone here is in the difference between something like "(f)+" and "(f+)". It has long been my question whether these two are somewhat different and, if the are, what that difference is. It looks like your answer has answered this my long-lasting question question:

Code: Select all

song := "kkkkkk*" ; there are 6 k's in this line
RegExMatch(song, "^(kkk)+\*", SubPat)  ; matches only 3 k's
msgbox %  SubPat1 
RegExMatch(song, "^(kkk+)\*", SubPat)  ; matches all six and seems to be the solution to my problem
msgbox %  SubPat1

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, barbaque, Bing [Bot], dagiccross, danrevella, Ian, Jrdixon_, Mager_man, TXShooter and 384 guests