Oh, but I think your gettok solution or the $remove solution is better if it's sure that the input will always be a number surrounded by [ ]. There's really no need for the regex, just wanted to show another approach.

Indeed, it captures digits inside [ and ].

More into detail: $regex($$1,/\[(\d+)\]/S)

* First part: input = $$1, double $$ so that the alias will stop if no input is specified

* Second part: the expression to match, enclosed between / /

--> the [ ] together have a special meaning inside a regex, because they define a character class. Therefore they need to be escaped, for instance by putting a \ backslash in front of them. Another way would be to enclose the string that you want escaped by putting that string between \Q and \E.

--> The brackets ( and ) are there around \d+, so that the matched string is captured, and can be referenced later on in the $regml

--> \d represents a digit 0-9, and the plus + means match 1 or more times, so here, the regex matches one or more digits. Since a regex is by default greedy, it will try to match as many numbers as it can, unless specified differently.

* Third part: the modifier, after the last slash /

--> The S modifier will strip the input string from control codes.

After calling the $regex with !.echo -q, if there is a match, there will be a reference to it in $regml(1). 1 because that is the first captured string in the expression. It could be useful to name a regex, so that references aren't overwritten by another regex call, though I didn't add it.

In a lot of cases, indeed, regex isn't really necessary, and solutions can be found with other tools.Then again, in other cases, they are absolutely indispensable, and provide an awesome tool.

Greets