Community discussions

MikroTik App
 
User avatar
ghostinthenet
newbie
Topic Author
Posts: 31
Joined: Sun Apr 04, 2021 1:36 pm
Location: Niagara-on-the-Lake, Canada
Contact:

Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 5:27 pm

Looking for the ability to use capturing groups in regex, which doesn't appear to be present in the OS.

Something like this would be ideal:

[admin@MikroTik] > :local testString "FQDN: host.documentation.net"; :if ( $testString ~"^FQDN: (.*)\$" ) do={ :put $1 }

Where $1 is the contents of the first capturing group, returning "host.documentation.net" at the end of the script.

If there's already a means to accomplish this, please let me know. I haven't been able to find much in my digging.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 6:03 pm

{
:local testString "FQDN: host.documentation.net"
:local removeThisAndAllBefore "FQDN: "
:put [:pick $testString ([:find $testString $removeThisAndAllBefore] + [:len $removeThisAndAllBefore]) [:len $testString]]
}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 6:35 pm

True, that works for the example.

But there can be multiple capture groups from a regex (and even named ones), so it could be used to extract multiple values from one string, and return the captured groups as an array. And that's where this be useful.

Still think it's a valid feature request, I even made one myself: viewtopic.php?t=131692#p907082

But there has not been a lot of progress on adding much* functionality to scripting in many years...

* only: :timestamp, :rndnum, :rndstr
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 18959
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 7:00 pm

Possibly due to no recent graduates from computer software engineering in Latvia..........OR
Cheap assed owners who dont want to hire the necessary staff to make MT really shine and sing!!
 
User avatar
ghostinthenet
newbie
Topic Author
Posts: 31
Joined: Sun Apr 04, 2021 1:36 pm
Location: Niagara-on-the-Lake, Canada
Contact:

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 8:41 pm

...there can be multiple capture groups from a regex (and even named ones), so it could be used to extract multiple values from one string, and return the captured groups as an array. And that's where this be useful.

Exactly. Returning the capturing groups' results as an array would be perfect.
 
User avatar
ghostinthenet
newbie
Topic Author
Posts: 31
Joined: Sun Apr 04, 2021 1:36 pm
Location: Niagara-on-the-Lake, Canada
Contact:

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 8:45 pm

{
:local testString "FQDN: host.documentation.net"
:local removeThisAndAllBefore "FQDN: "
:put [:pick $testString ([:find $testString $removeThisAndAllBefore] + [:len $removeThisAndAllBefore]) [:len $testString]]
}

That will cover the immediate requirement. Thanks!

(I would still like to see full capturing group support though. It's much easier than getting into string manipulation, especially if there are multiple matches to consider.)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 9:55 pm

My solution didn't mean that such a function shouldn't be implemented, like ":search" & ":replace"...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Feature Request - Regex Capturing Groups

Thu Mar 23, 2023 10:22 pm

My solution didn't mean that such a function shouldn't be implemented, like ":search" & ":replace"...
Fair enough. Regex group captures is pretty esoteric. Just string processing is annoying, they should do something. I'd take anything.

As you know, you can "string" together a solution to most things, it just time-consuming or a lot of code.

Who is online

Users browsing this forum: No registered users and 27 guests