Best way to learn RegEx for AHK?

Discuss other useful utilities, general computing tips & tricks, Internet resources, etc.
kunkel321
Posts: 162
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Best way to learn RegEx for AHK?

14 Jan 2016, 16:57

Hi Folks,
I've used regex a tiny bit and am trying to learn more, for a specific project I'm working on. From what I've read, the version of regex that ahk recognizes is fairly similar to other varieties, and is based on Perl. There are a couple free tools I've found for testing regexs I make. Of course there's Regex Tester by Robert Ryan, which is designed for ahk regex testing. But there are a couple of others out there, like Expresso and The Regex Coach.

I noticed that the expression for case-insensitivity is "i)" without quotes, at the beginning of the regex. This works for ahk, but Expresso says it's an error. So that's at least one difference. Are there likely to be lots of other differences too, or is it safe for me to use this as a learning tool? Also, there are a few regex books to be had at it-ebooks.info. But it's the same question: Is it safe to learn from those, or should I just stick to the Quick Reference guide in the AutoHotkey documentation?

Any other recommendations for learning?
Thanks in advance, for any tips.

EDIT: Actually, I see that Expresso has a setting to choose between C#, VisualBasic, Managed C++, or C++/CLI. Maybe one will be closer than the others? I know that AHK code, in general, pulls from several different languages....
kon
Posts: 1760
Joined: 29 Sep 2013, 17:11

Re: Best way to learn RegEx for AHK?

14 Jan 2016, 17:26

Usually I just write a mini script like this to test regex. That way you can be sure it works in AHK.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



I also like regex101.com. It allows you to save your regex and share it with others: https://regex101.com/r/vK0bJ8/1 The explanation it provides is pretty good too.
Note that options are listed in a separate box, which differs from the AHK syntax.

In terms of references, the majority of the time I use the help file's Quick Reference. There are some things found in the AHK help file Quick Reference that are unique to AHK. For example the "S" (study) option.

Sometimes a web-search works well if you are doing something complicated. Even if you find examples that aren't PCRE (perl compatible regular expressions), the differences are usually minor.


Other useful sites (but there are many more):
http://www.regular-expressions.info/
http://www.pcre.org/pcre.txt
Last edited by kon on 15 Jan 2016, 03:09, edited 2 times in total.
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Best way to learn RegEx for AHK?

15 Jan 2016, 03:08

Best way to learn RegEx for AHK?
Use it.

Use regex in whatever tools or languages you have a purpose for. Even if they are different flavours of regex, they're all based on the same concepts and generally have similar syntax.

From what I've read, the version of regex that ahk recognizes is fairly similar to other varieties, and is based on Perl.
You mean PCRE. Yes, it is similar to other varieties of regex, but PCRE is also very widely used directly by other programs and some languages. For instance, Notepad++ and PHP use PCRE.

I noticed that the expression for case-insensitivity is "i)" without quotes, at the beginning of the regex. This works for ahk, but Expresso says it's an error. So that's at least one difference.
The "options)" prefix is unique to AutoHotkey - other utilities or languages will have their own ways of specifying the same options, like check boxes or alternative syntax. Perl and Javascript use /pattern/i for regex literals. Forward-slash is used in place of quote marks.

Some of the option letters are copied directly from PCRE, and some of those correspond to Perl option letters. PCRE also supports changing them within the pattern (and therefore so do AutoHotkey and Notepad++):

Code: [Select all] [Download] GeSHi © Codebox Plus

         (?i)            caseless
(?J) allow duplicate names
(?m) multiline
(?s) single line (dotall)
(?U) default ungreedy (lazy)
(?x) extended (ignore white space)
(?-...) unset option(s)
Source: http://www.pcre.org/pcre.txt

Actually, I see that Expresso has a setting to choose between C#, VisualBasic, Managed C++, or C++/CLI.
What's the difference? Those are all .NET based. .NET has its own variety of regex, which as far as I'm aware, is the same across all .NET languages. I assume that Expresso itself is written in a .NET language, but PCRE can be used from .NET too...
Are there likely to be lots of other differences too[...]?
All of the syntax described in the PCRE manual should work. There are other differences between PCRE and .NET regex. I suggest you google for a comparison.
User avatar
joedf
Posts: 6434
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Best way to learn RegEx for AHK?

15 Jan 2016, 17:18

I use regexr to test them out. :)
kunkel321
Posts: 162
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: Best way to learn RegEx for AHK?

15 Jan 2016, 19:28

Thanks guys, for the tips. Ultimately I guess it's like Lexikos said, you just have to use it to learn it. The various tools are nice though, because you can immediately see what all the different matches are at once. Above, I mentioned Expresso, which is nice because of its "builder." I did notice in the manual though, that it's .net based. Regex Coach is PCRE based, but I do have to say that the online tools you guys suggested do provide nice descriptive info. Incidentally, searching for a comparison of the different flavors of regex quickly became overwhelming. It's all good though. I'm learning as I go. I'm also reading Jack Dunning's AutoHotkey Regex book on my Kindle... It's pretty good.

Kon, I noticed that your use of RegExMatch differs from what is shown in the ahk documentation:
RegExMatch(TestText, "[0-9]+:[0-9][0-9]", Match)
versus:
FoundPos := RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
It surpized me, because the docs don't list FoundPos := as optional. (They're not in brackets.)

I'm glad you posted it, because this is exactly what I want to do! (Extract time and date from a selected bit of text.. I don't really even care where its position is.) :mrgreen:

Thanks again guys!
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Best way to learn RegEx for AHK?

15 Jan 2016, 19:47

It surpized me, because the docs don't list FoundPos := as optional. (They're not in brackets.)
Optional parameters are typically enclosed in brackets in the documentation, but it's not an optional parameter. It's not a parameter at all.

The return value is not marked optional in any way because that's just how functions/expressions work. The function call and assignment are two separate operations. You don't have to assign the return value to a variable - you can pass it directly to another function or just discard it. FoundPos := is there just to give a more meaningful name than "return value", and let the user see at a glance what the return value means.
kunkel321
Posts: 162
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: Best way to learn RegEx for AHK?

31 Jan 2016, 11:36

Do you guys know if anyone has attempted to make a regex tester that will accept variables? For example, in the regex window you might have:
===========================================================================
reWDay := "i)\b((Mon|Tue(s)?|Wed(nes|s)?|Thu(rs|r)?|Fri|Sat(ur)?|Sun))(day|d)?\b"
(%reWDay% next week|next week %reWDay%)
===========================================================================
The top line(s) would be like a "resource" for the bottom line. And the bottom line would be the actual regex.
Would be kinda' clever..
In another thread there was discussion of utilizing this technique of pre-defining segments of regex code so that my Alternations would be more compact and easier to deal with. The problem is that I can't easily test them on chunks of code in-bulk.
User avatar
jackdunning
Posts: 35
Joined: 01 Aug 2016, 18:17

Re: Best way to learn RegEx for AHK?

21 Aug 2018, 07:15

For me, Ryan's RegEx Tester (as described in "Quick and Dirty Complex Text Replacement with Ryan’s RegEx Tester") offers the quickest way to learn the AutoHotkey Regular Expression functions RegExMatch() and RegExReplace() in an interactive environment. Also, the AutoHotkey Regular Expression Quick Reference acts as the best instantaneous overview of RegEx structure that I've seen.
Jack Dunning

I have a passion for helping new users grow maniacal about AutoHotkey Windows scripting.

Jack's AutoHotkey Blog
Free AutoHotkey Scripts and Apps for Learning Script Writing and Generating Ideas
AutoHotkey E-Books
burque505
Posts: 545
Joined: 22 Jan 2017, 19:37

Re: Best way to learn RegEx for AHK?

21 Aug 2018, 07:32

@jackdunning, I'm happy to see you posting here on the forum. I enjoy your blog posts a lot, and subscribe to the weekly digest.
I had not seen the RegEx post before, thanks.
Regards,
burque505
p.s. Looking forward to the next post in the LV Gui series.
burque505
Posts: 545
Joined: 22 Jan 2017, 19:37

Re: Best way to learn RegEx for AHK?

21 Aug 2018, 18:05

@garry, thanks very much for these RegEx links.
Helgef
Posts: 3151
Joined: 17 Jul 2016, 01:02
Contact:

Re: Best way to learn RegEx for AHK?

22 Aug 2018, 04:46

Another link, which has a lot of content, jeeswg's RegEx tutorial (RegExMatch, RegExReplace).

Cheers.
User avatar
hoppfrosch
Posts: 321
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: Best way to learn RegEx for AHK?

22 Aug 2018, 23:38

Some more Links - don't forget there are RegEx-Tester on the market written in pure AHK as well :D

* dR's Simple RegExTester by derRaphael
* RegExHelper by Delta Pythagorean
* RegExStar by WizardZedd0
User avatar
joedf
Posts: 6434
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: Best way to learn RegEx for AHK?

23 Aug 2018, 06:42

There’s also one in AutoGUI I think :think:
And the online http://Regexr.com

Return to “Other Utilities & Resources”

Who is online

Users browsing this forum: No registered users and 5 guests