RegEx handy examples (RegExMatch, RegExReplace)

Helpful script writing tricks and HowTo's
User avatar
jeeswg
Posts: 1227
Joined: 19 Dec 2016, 01:58
Location: UK

RegEx handy examples (RegExMatch, RegExReplace)

11 Feb 2017, 22:16

I've tried to collect all of the most important
RegEx techniques that I've used or may like to use.

Please notify of any corrections, improvements/simplifications.
Do post any handy examples not included, or links to other examples.
Also do mention any useful techniques doable in RegEx but not mentioned in AutoHotkey's help.

Btw if other people want to create their own lists of RegEx handy samples,
including some that are closely/loosely based on mine, please go ahead and post your link here.
For example at the extreme end: copying the code, but only changing the variable names, is fine.

522 lines (initially)

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



LINKS:

extracting items from a list using RegEx (various methods) (get all matches) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30448

simplest way to make a RegEx needle literal? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30420

[PCRE REGULAR EXPRESSION SYNTAX SUMMARY]
[for syntax not mentioned in https://autohotkey.com/docs/misc/RegEx-QuickRef.htm]
pcresyntax specification
http://www.pcre.org/original/doc/html/pcresyntax.html

[the \G anchor, not mentioned in https://autohotkey.com/docs/misc/RegEx-QuickRef.htm ]
Add Thousands Separator - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/50019-add-thousands-separator/

[TIPS] A collection/library of regular expressions - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/12374-tips-a-collectionlibrary-of-regular-expressions/

Regular Expressions: a simple, easy tutorial
http://phi.lho.free.fr/programming/RETutorial.en.html

Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript
https://regex101.com/

Tutorial: An AHK Introduction to RegEx - Tutorials - AutoHotkey Community
https://autohotkey.com/board/topic/39733-tutorial-an-ahk-introduction-to-regex/

AutoHotkey Expression Examples: "" %% () and all that
http://www.daviddeley.com/autohotkey/xprxmp/autohotkey_expression_examples.htm#N
Last edited by jeeswg on 13 Jun 2017, 10:25, edited 26 times in total.
noname
Posts: 301
Joined: 19 Nov 2013, 09:15

Re: RegEx handy examples (RegExMatch, RegExReplace)

16 Feb 2017, 11:09

Nice collection ,i already found some i will use.Thanks for posting :)
kon
Posts: 1716
Joined: 29 Sep 2013, 17:11

Re: RegEx handy examples (RegExMatch, RegExReplace)

16 Feb 2017, 11:37

This looks like a useful collection.

Just some suggestions, do with them what you will:

";Greed: By default..." -- This line is the only one that is very long. Perhaps some linefeeds are in order?

vList is used throughout. I suggest giving it a value of some sample text. Then add comments to each regular expression showing the expected result. With a few modifications, this could be an actual working script that runs and shows the user the results of each regular expression.

The lack of indenting is painful for me to read, but I realize this is just personal preference. I don't think I'm in the minority though.

Sites like https://regex101.com/ are worth mentioning IMO. There are many, but that's the one I use. They are especially helpful when answering "Ask for Help" questions because you can save a link to your regular expression and post it for others to read. They explain what each symbol does, which saves a lot of typing-out explanations.

Edit:

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

;Is it possible to achieve the AutoHotkey RegEx 'options',
;by putting text in the needle proper?
;e.g. i m s U (case-insensitive matching, multiline, DotAll, ungreedy)
;e.g. `n `r `a
;see: https://autohotkey.com/docs/misc/RegEx-QuickRef.htm#Options

See https://www.autohotkey.com/docs/misc/Re ... htm#subpat specifically "Change options on-the-fly..."
User avatar
jeeswg
Posts: 1227
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx handy examples (RegExMatch, RegExReplace)

16 Feb 2017, 12:35

@noname
Thanks so much for you comments.
At the moment AHK v2 doesn't have if var in/contains/is type,
(I believe,) which had prompted me to seek various RegEx methods
in the meantime, leading to this collection.

@kon
[UPDATE:]
I split the line ';Greed: By default...'
I added indentation.
I added https://regex101.com/.
() I'm still to add in vText in more places.

@kon
'Greed: By default'
That line is long, it's a quote from the help,
I might split it up.

'vList is used throughout'
I used examples with vList at it happens,
I think you mean vText.
That's a tricky one, because that would double/triple
the lines used, and really bulk everything out.
To begin with at least, there are some that would definitely benefit
from example text, like the 'remove columns' ones,
so I'll add example text to those.

'The lack of indenting'
I counted 15 lines starting with 'if ',
with only 7 followed by lines (which would thereby
require indentation), in basic 2-line/4-line blocks.
[EDIT: and 7 lines beginning with 'Loop']
I'm sympathetic to people wanting indentation for
larger scripts.
It wouldn't be too hard for me to add that in,
and I might do, I'm curious though, is:

so much worse than:

People seem to be really allergic to non-indentation.
I think there should be a poll on this forum,
as to whether I should use indentation,
I've heard it a fair amount.
I don't mind when people don't use indentation,
and I use Notepad, so I get zero assistance when reading scripts.
Btw these comments on indentation are aimed at everyone,
not you specifically, just that I hadn't got round to posting them.

To be honest I see indentation as a silly coding fad,
that often makes code less readable.
I do use it sometimes, when I have 2 or 3 pairs of curly brackets.
It definitely helps in those situations.
It seems to be pretty widespread, the demand for indentation,
I'm working on functions to add it in for my big scripts,
and library functions before I share them (together with manual checking),
even though personally I think over-indentation is unnecessary and undesirable,
a mark of indoctrination, with blank lines, comments and good variable names
being far more important.
I also like to use barriers (e.g. equal signs), uppercase comments e.g. ';STAGE 1 -',
and notations like ';;;SECTION' in some instances (3 semicolons being easy to search for).
Haha I'm going hard on indentation, because I've seen what looks like
some scary groupthink on the matter, I'm not particularly ideological re. programming.

Thanks so much for your comments, it means a lot coming
from you, you've done some clever things on this forum.
Last edited by jeeswg on 19 Feb 2017, 12:47, edited 2 times in total.
kon
Posts: 1716
Joined: 29 Sep 2013, 17:11

Re: RegEx handy examples (RegExMatch, RegExReplace)

16 Feb 2017, 12:58

Yes, I meant vText.

I edited my post right before you posted. Not sure if you saw my edit so I'll just point it out here.

Regarding the sample text and expected results.
I've answered a lot of "Ask for Help" questions on regex. They usually play-out in two ways.
1. The OP provides sample text for both the haystack and the expected results.
2. OP tries to explain the problem in plain English.

#1 is usually answered with one reply.
#2 usually becomes a "moving target" style question as people post solutions that appear to conform to OP's request, but due to OP's lack of understanding of regex the solutions need to be adjusted to satisfy new requirements. These threads tend to drag on for quite a while.

I think this is a good argument for any regex tutorial to have sample text and show the expected results. It doesn't have to increase the length of the tutorial by 2-3x. Maybe just add a comment to the same line?
User avatar
jeeswg
Posts: 1227
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx handy examples (RegExMatch, RegExReplace)

16 Feb 2017, 13:14

Hmm, classic insight.
Yes I've thought for a while that the antidote to manuals being difficult
to understand in a foreign language,
is lots of before/after examples.
Actually even if the manual is in your language.

Wow, one magical haystack, will think of what I can do.

It was exhausting producing this tutorial, although I needed it for my own use anyway,
yeah, the way I like to do things, is get something quality *finished*,
and then eventually have a rethink and come back to it,
when you can bear to re-explore the material, and have possibly come up against some new ideas.

[EDIT:]
Thanks for the link re. 'Change options on-the-fly', I had noticed that but not understood it at the time. I found it mentioned here under Options (PhiLho was quite good with this RegEx stuff):
Regular Expressions: a simple, easy tutorial
http://phi.lho.free.fr/programming/RETutorial.en.html

I've updated the queries section to reflect this.
guest3456
Posts: 1974
Joined: 09 Oct 2013, 10:31

Re: RegEx handy examples (RegExMatch, RegExReplace)

18 Feb 2017, 09:04

jeeswg wrote:People seem to be really allergic to non-indentation.
I think there should be a poll on this forum,
as to whether I should use indentation,
I've heard it a fair amount.
I don't mind when people don't use indentation,
and I use Notepad, so I get zero assistance when reading scripts.
Btw these comments on indentation are aimed at everyone,
not you specifically, just that I hadn't got round to posting them.

To be honest I see indentation as a silly coding fad,
that often makes code less readable.


lol. yet another example of multiple people telling you that you're wrong, and you continuing to be stubborn and hardheaded. are you ever going to come down from your high horse? you would probably get the same responses on stackoverflow, and then complain that "those arrogant stackoverflow people deleted my post because i didn't indent my code" or some other nonsense. no poll is necessary. this is one of the most basic style aspects of programming, regardless of the langauge. god forbid you ever use Python, where indentation is REQUIRED

but, if you like the fact that no one reads your posts or your code, then continue with what you're doing

if however you actually want input from others, you should probably present your code in ways that they want to read it.


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 3 guests