# Regex: How to get matched content?

**URL:** <https://forum.mikrotik.com/t/regex-how-to-get-matched-content/138798>\
**Category:** Scripting\
**Created:** [April 22, 2020, 6:50pm UTC](https://forum.mikrotik.com/t/regex-how-to-get-matched-content/138798 "2020-04-22T18:50:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eguun](https://forum.mikrotik.com/letter_avatar/eguun/32/6_449c533f586939b6e5f246632a46421f.png) [@eguun](https://forum.mikrotik.com/u/eguun)\
**Post date:** [April 22, 2020, 6:50pm UTC](https://forum.mikrotik.com/t/regex-how-to-get-matched-content/138798/1 "2020-04-22T18:50:49Z")

</div>

Hi,

I am not sure Mikrotik support this basic function.

The need is to parse a string and extract some content based on regex.  
I think mikrotik can only return true/false if the regex is matched, but can’t return $1, $2 … based on the matches (like any other coding language would be capable of doing).

For example the script below, where I would like the script to return the regex match between parenthesis.  
$1 returns nothing

```plaintext
:local regexTestString "my test string bla bla"

:if ( $regexTestString ~".*(test.*)" ) do={
	:put "regex match: $1"
}

```

returns:

> regex match:

I have searched through the forum and google.  
Very few results. The closest I was able to find was a response to an open discussion on feature requests, but no conclusion or follow-up:  
[http://forum.mikrotik.com/t/built-in-function-library/117288/2](http://forum.mikrotik.com/t/built-in-function-library/117288/2)

**Is there something obvious I’m missing, or is the feature simply absent?**

Thanks

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [April 23, 2020, 6:58am UTC](https://forum.mikrotik.com/t/regex-how-to-get-matched-content/138798/2 "2020-04-23T06:58:53Z")

</div>

What you is looking for is called capturing group.  
It des not look like RouterOS support capturing group or named capturing group.

```plaintext
{
:local regexTestString "my test string bla bla"

:if ( $regexTestString ~".*(?<tt>test.*)" ) do={
	:put "regex match: $tt"
}
}

```

RouterOS regex info:  
[https://wiki.mikrotik.com/wiki/Manual:Regular\_Expressions](https://wiki.mikrotik.com/wiki/Manual:Regular_Expressions)

---

<div class="post-metadata">

**Author:** ![eguun](https://forum.mikrotik.com/letter_avatar/eguun/32/6_449c533f586939b6e5f246632a46421f.png) [@eguun](https://forum.mikrotik.com/u/eguun)\
**Post date:** [April 23, 2020, 7:24am UTC](https://forum.mikrotik.com/t/regex-how-to-get-matched-content/138798/3 "2020-04-23T07:24:54Z")

</div>

Thanks for your response,

I guess each language can have its own naming next to this feature, “capturing group” being one of them.  
Perl calls it “extracting matches”, boen\_robot in his post referenced earlier called it “regexmatch”  
If it gets returned as $1 $2 would already be good (ie: without the capacity to tag each return with a distinctive name)

Not great to have this as unsupported considering that Mikrotik OS initially offsprings from a Linux distribution

Thanks for your update on the script.  
I don’t get anything returned when running it, which is I guess expected as you called out this feature as being “unsupported”

I obviously went to the regex page of mikrotik, but it’s really short of ~20 lines.  
It only states it follows POSIX (with 5 listed exceptions in bullet points), and list 8 areas where the Mikrotik device could use regex.  
That’s it. There are no screenshot, implementation examples, scripts templates.

Eventually my workaround was partial, suboptimal, and involved nested string split/pick

Cheers
