Some Basic Regex group Capture / Non-Capture Questions Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Some Basic Regex group Capture / Non-Capture Questions

11 May 2019, 19:22

I am still relatively new to Regular Expressions.

Can somebody please demonstrate how to use a non-capture group in a function. For example:

Code: Select all

String := "Hello World"
I know that if I use the following:

Code: Select all

var := RegexReplace(string, "(Hello)/s(World)","$2")
Result: "World"... Easy-Peasy, but suppose I want to use a non-capture group. I am confused as how to make it work. I want to capture "World" by ignoring "Hello". So far I tried:

Code: Select all

pos := RegexMatch(string, "(?:Hello/s)World)", var) ;?: as in ignore "Hello"
Nevertheless, the result is still "Hello World". So what am I doing wrong here?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Some Basic Regex group Capture / Non-Capture Questions  Topic is solved

11 May 2019, 20:05

A_Perry_1984 wrote:
11 May 2019, 19:22
Regexes are not my cup of tea - far from it: someone might be able to clarify in details if needed. Anyway, It depends what you have in mind by 'by ignoring "Hello"' - here's some examples that come to my mind:

Code: Select all

string := "Hello World"
pos := RegExMatch(string, "(Hello)\s(World)", var)
MsgBox % pos "," var2
pos := RegexMatch(string, "(?:Hello)\s(World)", va)
MsgBox % pos "," va1
pos := RegexMatch(string, "Hello\s\KWorld", v) ;  \K causes any previously-matched characters to be omitted from the final matched string (here the whole string).
MsgBox % pos "," v
Btw its \s (backslash character).

Hope this helps.
my scripts
StefOnSteroids
Posts: 77
Joined: 08 Aug 2015, 10:22

Re: Some Basic Regex group Capture / Non-Capture Questions

11 May 2019, 21:54

Code: Select all

String := "Hello World"
MsgBox, % RegExReplace(String, "(Hello\s)(World)", "$1")
MsgBox, % RegExReplace(String, "(?:Hello\s)(World)", "$1")
Both messages return the first group.
And yet, the first "captured" group in the second example is "World" and not "Hello ", because the first group does not even count as group 1. It is not "captured" at all.
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Some Basic Regex group Capture / Non-Capture Questions

11 May 2019, 22:57

I do not care whatever you have in your mind about Capture/Non-Capture, just enjoy..

Code: Select all

_ := "Hello World"
RegExMatch(_, "(?<=Hello )World", x)
MsgBox % ""
.  x "`n"
.  RegExReplace(_, "Hello (?=World)")
and make all your previous help post with Green Check if you found a good answer (of course including this one)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 02:08

See \K and (?<=...) [look-behind], mentioned here:
Regular Expressions (RegEx) - Quick Reference | AutoHotkey
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm

Code: Select all

string := "Hello World"
pos := RegExMatch(string, "Hello\s\KWorld", var)
MsgBox, % pos "," var
pos := RegExMatch(string, "(?<=Hello\s)World", var)
MsgBox, % pos "," var
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 04:19

It seems to me, in light of look aheads, behinds, and groups, there is really no purpose to a non-capture group. For that matter I'm not sure they do anything in AHK to begin with. Thank you everyone! These are so helpful.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 05:00

well they exist so there certainly must be some kind of a purpose to them. for example, they can be repeated, unlike lookaheads
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 05:21

Sometimes you need to use parentheses to contain something, e.g. (abc|def|ghi), but, by doing that, that would interfere with the indexes of any other numbered subpatterns.

A fix is to use ?:, e.g. (?:abc|def|ghi).

I hardly ever use it, the only example I could think of was when identifying variables: "(?:[_A-Za-z]|[^[:ascii:]])(?:\w|[^[:ascii:]]){0,252}".
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 05:25

Good
I like the Green Check Mark

and
some of RegEx contents are not really needed for the beginner level
each level has its own ways of solving problem

ah, I am at the beginner level. Do not misunderstand me.

Good Luck To You
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 09:40

jeeswg wrote:
12 May 2019, 05:21
Sometimes you need to use parentheses to contain something, e.g. (abc|def|ghi), but, by doing that, that would interfere with the indexes of any other numbered subpatterns.

A fix is to use ?:, e.g. (?:abc|def|ghi).

I hardly ever use it, the only example I could think of was when identifying variables: "(?:[_A-Za-z]|[^[:ascii:]])(?:\w|[^[:ascii:]]){0,252}".
I see your point. I don't think that would something needed very often.
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Some Basic Regex group Capture / Non-Capture Questions

12 May 2019, 09:47

Klarion wrote:
12 May 2019, 05:25
Good
I like the Green Check Mark

and
some of RegEx contents are not really needed for the beginner level
each level has its own ways of solving problem

ah, I am at the beginner level. Do not misunderstand me.

Good Luck To You
No worries. I get what your saying. I don't think regex is that complicated to learn, so much as figuring out the limitless ways to manipulate text with it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], peter_ahk and 126 guests