Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the zsh equivalent for $BASH_REMATCH[]?

Tags:

zsh

What is the equivalent in zsh for $BASH_REMATCH, and how is it used?

like image 929
Tom Hale Avatar asked Oct 28 '18 09:10

Tom Hale


People also ask

What is the difference between zsh and Bash?

Interactive configuration features have many differences while comparing Zsh and Bash. Zsh is known for its extensibility, good customization, and advanced features. Since Zsh is made from Bash, almost 90% of the scripting is similar, and it is compatible with Bash. Both Zsh and Bash have many similarities and are easily portable.

Is zsh good for programming?

Zsh is known for its extensibility, good customization, and advanced features. Since Zsh is made from Bash, almost 90% of the scripting is similar, and it is compatible with Bash. Both Zsh and Bash have many similarities and are easily portable.

Where is the match array in Bash stored?

From the source: The GNU bash manual, Conditional Constructsand Bash Variables. The BASH_REMATCHArray If the latest [[]]-expression matched the string, the matched part of the string is stored in the BASH_REMATCHarray.

How do I compare a string to a regular expression in Bash?

Bash: Using BASH_REMATCH to pull capture groups from a regex The =~ binary operator provides the ability to compare a string to a POSIX extended regular expression in the shell.


2 Answers

To make zsh behave the same as bash, use:

setopt BASH_REMATCH

Or within a function consider:

setopt local_options BASH_REMATCH

(this will only set the option within the scope of the function)

Then just use $BASH_REMATCH as you would in bash.


The manual says about BASH_REMATCH:

When set, matches performed with the =~ operator will set the BASH_REMATCH array variable, instead of the default MATCH and match variables. The first element of the BASH_REMATCH array will contain the entire matched text and subsequent elements will contain extracted substrings. This option makes more sense when KSH_ARRAYS is also set, so that the entire matched portion is stored at index 0 and the first substring is at index 1. Without this option, the MATCH variable contains the entire matched text and the match array variable contains substrings.

Then =~ will behave like in bash, but if you want the full behaviour as described in the manual:

string =~ regexp

true if string matches the regular expression regexp. If the option RE_MATCH_PCRE is set regexp is tested as a PCRE regular expression using the zsh/pcre module, else it is tested as a POSIX extended regular expression using the zsh/regex module. Upon successful match, some variables will be updated; no variables are changed if the matching fails.

If the option BASH_REMATCH is not set the scalar parameter MATCH is set to the substring that matched the pattern and the integer parameters MBEGIN and MEND to the index of the start and end, respectively, of the match in string, such that if string is contained in variable var the expression ‘${var[$MBEGIN,$MEND]}’ is identical to ‘$MATCH’. The setting of the option KSH_ARRAYS is respected. Likewise, the array match is set to the substrings that matched parenthesised subexpressions and the arrays mbegin and mend to the indices of the start and end positions, respectively, of the substrings within string. The arrays are not set if there were no parenthesised subexpresssions. For example, if the string ‘a short string’ is matched against the regular expression ‘s(...)t’, then (assuming the option KSH_ARRAYS is not set) MATCH, MBEGIN and MEND are ‘short’, 3 and 7, respectively, while match, mbegin and mend are single entry arrays containing the strings ‘hor’, ‘4’ and ‘6’, respectively.

If the option BASH_REMATCH is set the array BASH_REMATCH is set to the substring that matched the pattern followed by the substrings that matched parenthesised subexpressions within the pattern.

like image 199
Tom Hale Avatar answered Oct 24 '22 22:10

Tom Hale


Alternatively, one could simply use

$match[1]

in place of

$BASH_REMATCH[1]
like image 25
Meng Lu Avatar answered Oct 24 '22 21:10

Meng Lu