Regex: trying to make part optional... Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 1120
Joined: 30 Nov 2015, 21:19

Regex: trying to make part optional...

05 Apr 2023, 14:23

It's for capturing hotstrings...
(:(\*|\?|\w)*:)(..*)(::)(.*\b)(\s*\;.*)?

Really I want to isolate the trigger string and the replacement text. I'm working with single lines of text, so no need to worry about newline characters. I can't just capture everything after "::" because I would erroneously capture any comment.

Presumably the rule for a comment is, <one or more white spaces><semicolon><zero or more of any other printable characters> And the whole part should be optional (not every hotstring has a comment).

Here is my subpattern: (\s*\;.*)? The question mark should make it optional, yes? I tried enclosing that whole part in extra parenths, but no joy.

Thoughts?

test code:

Code: Select all

#SingleInstance, Force
; AHK v1.
varMain =
(
::aaa::blah ;comment
::bbb::blah
:T:ccc::Biz ;;;;;; :unreachable
::aaa::blua
:*:cc::Wap ; makes above unreachable
::ddd::bluab
::eee::Boo
:B0C:ccc::Biz ; 101 numbers in my comment!!!!
::ccc::Biz
::fff::blah
::ggg::Foo ;unreachable
::eee::Bang
:*B0:gg::Bar ; makes above unreachable
)
;~ regex := "(:(\*|\?|\w)*:)(..*)(::)(.*\b)(\s*\;+.*)" ; works if comment present... But no comment = no match
;~ regex := "(:(\*|\?|\w)*:)(..*)(::)(.*\b)(\s*\;.*)" ; works, but comment is not optional.
regex := "(:(\*|\?|\w)*:)(..*)(::)(.*\b)(\s*\;.*)?" ; comment is captured with replacement text.
;~ OutArrays
;~ 1 = :*:
;~ 3 = trigger
;~ 5 = expansion
;~ 6 = comment

Loop, parse, varMain, `n, `r ; Check contents line-by-line.
{
	If RegExMatch(A_LoopField, regex, result)
		MsgBox % "loop " A_Index " item:`n" A_LoopField "`n`n1 " result1 "`n 2 " result2 "`n 3 " result3 "	`n 4 " result4 "	`n 5 |" result5 "|	`n 6 " result6 "`nall " result
	else
		MsgBox % "loop " A_Index " item:`n" A_LoopField "`n`nNO MATCH"
}

Esc::ExitApp
ste(phen|ve) kunkel
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Regex: trying to make part optional...  Topic is solved

05 Apr 2023, 17:42

Code: Select all

(:(\*|\?|\w)*:)(..*)(::)(.*?)(?=\s;|$)\s*(;.*)?
                        ^^^^^ - start matching the 'ReplacementText' lazily...
                             ^^^^^^^^^ - ...until u find what would be the start of a single line comment or the end of the string
                                      ^^^ - discard trailing whitespace after 'ReplacementText'
                                         ^^^^^^ - match a single-line comment, if there was one
User avatar
kunkel321
Posts: 1120
Joined: 30 Nov 2015, 21:19

Re: Regex: trying to make part optional...

05 Apr 2023, 19:04

Thanks Swagfag! And thanks for the explanations. It's clever having the 'end-of-line' dollar sign in an alternation group with the semicolon. I like your use of up arrow circumflexes ^^^^ to attach the explanations to the regex components... I'll have to remember that for future use. :thumbup:

Edit: I made a couple of minor tweaks to not capture unneeded parts... Not sure if this will make the regex faster, but I guess it can't hurt to avoid capturing unneeded data.

Code: Select all

regex := "(:(?:\*|\?|\w)*:)(..*)::(.*?)(?=\s;|$)\s*(;.*)?" ; swagfag-based
;~ OutArrays
;~ 1 = :options:
;~ 2 = trigger
;~ 3 = expansion when present
;~ 4 = comment when present
ste(phen|ve) kunkel

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, skolomir and 116 guests