Bash =~: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 6: Line 6:
The string to the right of the = ̃ operator is considered an extended regular expression and matched against the string to the left of the operator. The rules governing the extended regular expression are described in regex(3).
The string to the right of the = ̃ operator is considered an extended regular expression and matched against the string to the left of the operator. The rules governing the extended regular expression are described in regex(3).
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
if [["string_to_be_matched_against_regex" =~ regex ]]; then
if [[ "string_to_be_matched_against_regex" =~ regex ]]; then
   # match  
   # match  
else
else
Line 15: Line 15:
Usage is subject to the following constraints:
Usage is subject to the following constraints:
* The use of &#91;[...]] is required.
* The use of &#91;[...]] is required.
* ''DO NOT use double quotes when specifying the regular expression, just specify the regex literal directly.''
* ⚠️''DO NOT use double quotes when specifying the regular expression, just specify the regex literal directly.'' If you do double quote the right-hand side string, =~ will still work but it will match the quoted string literally rather than a regular expression.
* Variable expansion is performed, the following expression is valid:
<syntaxhighlight lang='bash'>
if [["something" =~ ${regex} ]]; then ...
</syntaxhighlight>
* If the regular expression contains spaces, use single quotes to denote space:
<syntaxhighlight lang='bash'>
if [["something" =~ ${regex}' ' ]]; then ...
</syntaxhighlight>
This also works:
<syntaxhighlight lang='bash'>
[[ "${output}" =~ "chart type".*"required" ]]
</syntaxhighlight>
* Variable substitution works for both the string to be matched and the regular expression. When using a variable to specify the regular expression, do not enclose the variable in quotes, use it directly as in the example below:
<syntaxhighlight lang='bash'>
if [[ "${var_containing_string_to_be_matched_against_regex}" =~ ${var_containing_regex} ]]; then ...
</syntaxhighlight>
* It matches &#91;[:space:]]
* If any part of the pattern is quoted, it is forcibly matched as a string.
* The return value is 0 if the string matches the pattern, and 1 otherwise.
* If the regular expression is syntactically incorrect, the conditional expression's return value is 2.
* If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.
* The =~ operator has the same precedence as == and !=.
 
=Metacharacters=
 
See man re_format.
 
Single digits:
 
<syntaxhighlight lang='text'>
[[:digit:]]
</syntaxhighlight>
 
Multiple digits:
 
<syntaxhighlight lang='text'>
[[:digit:]]+
</syntaxhighlight>
 
=Examples=
 
A string that does not contain a slash:
 
<syntaxhighlight lang='bash'>
if [[ ${something} =~ ^[^/]*$ ]]; then
  ...
fi
</syntaxhighlight>
 
Multiple spaces at the end:
 
<syntaxhighlight lang='bash'>
if [[ ${something} =~ ^blah' '*$ ]]; then
  ...
fi
</syntaxhighlight>
 
=TODO=
 
<font color=darkgray>regex(3)</font>

Latest revision as of 02:24, 26 June 2021

Internal

Overview

The string to the right of the = ̃ operator is considered an extended regular expression and matched against the string to the left of the operator. The rules governing the extended regular expression are described in regex(3).

if [[ "string_to_be_matched_against_regex" =~ regex ]]; then
  # match 
else
  # no match 
fi

Usage is subject to the following constraints:

  • The use of [[...]] is required.
  • ⚠️DO NOT use double quotes when specifying the regular expression, just specify the regex literal directly. If you do double quote the right-hand side string, =~ will still work but it will match the quoted string literally rather than a regular expression.
  • Variable expansion is performed, the following expression is valid:
if [["something" =~ ${regex} ]]; then ...
  • If the regular expression contains spaces, use single quotes to denote space:
if [["something" =~ ${regex}' ' ]]; then ...

This also works:

[[ "${output}" =~ "chart type".*"required" ]]
  • Variable substitution works for both the string to be matched and the regular expression. When using a variable to specify the regular expression, do not enclose the variable in quotes, use it directly as in the example below:
if [[ "${var_containing_string_to_be_matched_against_regex}" =~ ${var_containing_regex} ]]; then ...
  • It matches [[:space:]]
  • If any part of the pattern is quoted, it is forcibly matched as a string.
  • The return value is 0 if the string matches the pattern, and 1 otherwise.
  • If the regular expression is syntactically incorrect, the conditional expression's return value is 2.
  • If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.
  • The =~ operator has the same precedence as == and !=.

Metacharacters

See man re_format.

Single digits:

[[:digit:]]

Multiple digits:

[[:digit:]]+

Examples

A string that does not contain a slash:

if [[ ${something} =~ ^[^/]*$ ]]; then
   ...
fi

Multiple spaces at the end:

if [[ ${something} =~ ^blah' '*$ ]]; then
   ...
fi

TODO

regex(3)