RegEx Question Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
foxhunter
Posts: 72
Joined: 04 Aug 2016, 04:27

RegEx Question

20 Jan 2018, 08:03

Hello,

I need help for regex expressions. I want to remove all Lines with having a trailing Backslash and starting with two spaces using this code:

Code: Select all

needle:= "m)  .*?\\\R"

haystack=
(
Line01
  Line02
  Line03\
Line04\
  Line05\
Line06\
)

NewStr := Regexreplace(haystack, needle)

msgbox %NewStr%
The result for Newstr is:

Code: Select all

NewStr = Line01
Line04\
Line06\
"Line02" was also deleted, but was not intended to.
What is wrong in my code?

Another question: When changening \R to $ it does not work either. I though with multline option m) this should be the same?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: RegEx Question  Topic is solved

20 Jan 2018, 09:13

Code: Select all

needle:= "m`a)^  .*?\\\R?"
Last edited by Odlanir on 20 Jan 2018, 10:10, edited 1 time in total.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: RegEx Question

20 Jan 2018, 09:16

try this

Code: Select all

text=
(join`r`n
Line01
  Line02
  Line03\
Line04\
  Line05\
Line06\
)

msgbox % RegExReplace(text, "m)  .*\\")
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: RegEx Question

20 Jan 2018, 09:34

Sorry, I amended my code:

Code: Select all

text=
(join`r`n
Line01
  Line02
  Line03\
Line04\
  Line05\
Line06\
  Line07\
)

msgbox % RegExReplace(text, "m)^  .*\\(\r\n)?")		;"\r\n" is optional, otherwise, "Line07" would not be removed
foxhunter
Posts: 72
Joined: 04 Aug 2016, 04:27

Re: RegEx Question

20 Jan 2018, 12:26

Oh I see, both solutions works !

Maybe you could comment out a little bit for understanding, as it is unclear to me :
- Why `a is required in the regex options, but \R does not (" \R means "any single newline of any type", namely those listed at the `a option")?
- Why join`r`n is needed, although there are 'r'n at each end of the line of variable declaration?

Thanks for your help.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: RegEx Question

21 Jan 2018, 09:55

foxhunter wrote: Why join`r`n is needed, although there are 'r'n at each end of the line of variable declaration?
Because by default, AutoHotkey uses only "`n" to join the lines inside "brackets", so you will have:

Line01`n Line02`n Line03\`nLine04\`n Line05\`nLine06\`n Line07\`n

by using "join`r`n", you will have:

Line01`r`n Line02`r`n Line03\`r`nLine04\`r`n Line05\`r`nLine06\`r`n Line07\`r`n
foxhunter wrote: Why `a is required in the regex options, but \R does not (" \R means "any single newline of any type", namely those listed at the `a option")?
- Windows interprets "`r`n" as new line -
- Linux interprets "`n" as new line -
- Classic Mac OS interprets "`r" as new line -

by default, regex interprets only "`r`n" as new line, but when "`a" is in use, regex recognizes any type of newline, namely `r, `n, `r`n, `v/VT/vertical tab/chr(0xB), `f/FF/formfeed/chr(0xC), and NEL/next-line/chr(0x85).

so, in your first example, since you didn't use "join`r`n" nor "`a" option, the regex multi-line option "m" interprets all the string inside "haystack" variable as an unique line!

Examples:

Line01`n Line02`n Line03\`nLine04\`n Line05\`nLine06\`n Line07\`n

- "m)" regex interprets all the string above as an unique line!
- "m`a)" regex detects 7 lines from the string above

Line01`r`n Line02`r`n Line03\`r`nLine04\`r`n Line05\`r`nLine06\`r`n Line07\`r`n

- "m)" regex detects 7 lines from the string above
- "m`a)" regex detects 7 lines from the string above
foxhunter
Posts: 72
Joined: 04 Aug 2016, 04:27

Re: RegEx Question

23 Jan 2018, 05:02

Thanks for the explanation!!!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: RegEx Question

23 Jan 2018, 09:37

foxhunter wrote:Thanks for the explanation!!!
You welcome!

(Note that my explanation may not be 100% accurate, so, you should ask regex experts from this forum for better explanations!)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GollyJer, Lamron750, septrinus, shawn_xwang and 243 guests