在Bash正则表达式中,`^`和`$`是指行还是整个字符串? [英] In Bash regular expressions do `^` and `$` refer to lines, or to the entire string?
问题描述
在 Linux文档项目中(我没有在 Bash手册)中找不到有关正则表达式元字符的详细信息,元字符 ^
和 $
被定义为匹配行:
In The Linux Documentation Project (I didn't find details about the regex metacharacters in the Bash manual), the metachars ^
and $
are defined as matching lines:
^
:匹配行[...]
开头的空字符串$
:匹配行尾的空字符串
^
: Matches the empty string at the beginning of a line [...]
$
: Matches the empty string at the end of a line
但是,当我尝试时,这是不正确的:
however, when I try, this is not correct:
$ string="a
> b
> c"
$ [[ $string =~ ^a ]] && echo BOS match
BOS match
$ [[ $string =~ ^b ]] && echo BOL match
# nothing
这些手册真的不对吗,还是我遗漏了一些东西?
Are the manuals really wrong, or I am missing something?
推荐答案
^
匹配整个输入字符串的开头,而 $
匹配整个输入字符串的结尾POSIX正则表达式(Bash使用POSIX ERE).您链接到的文档中提到行,因为大多数文本处理工具(例如 sed
, grep
或 awk
)默认情况下逐行读取输入,并且在大多数情况下,字符串与该行重合.
^
matches start of the whole input string and $
matches the end of the whole input string in a POSIX regex (Bash uses POSIX ERE). The document you link to mentions lines because most text processing tools, like sed
, grep
or awk
read the input line by line by default, and string coincides with the line in the majority of cases.
请参见 POSIX正则表达式文档:
9.3.8 BRE表达锚定
可以将BRE限制为匹配以行开头或结尾的字符串.这就是所谓的锚定".在以下情况下,应将抑扬符和美元符号的特殊字符视为BRE锚点:
9.3.8 BRE Expression Anchoring
A BRE can be limited to matching strings that begin or end a line; this is called "anchoring". The circumflex and dollar sign special characters shall be considered BRE anchors in the following contexts:
-
回旋符(
'^'
)在用作整个BRE的第一个字符时应为锚.当用作子表达式的第一个字符时,该实现可以将抑扬符视为锚点.回旋符号应将表达式(或子表达式)锚定到字符串的开头;BRE只能匹配从字符串的第一个字符开始的序列.例如,BRE"^ ab"匹配字符串"abcdef"中的"ab",但不匹配字符串"cdefab".BRE(^ ab)"可能与前一个字符串匹配.便携式BRE必须在子表达式中避开领先的抑扬符,以匹配文字抑扬符.
A circumflex (
'^'
) shall be an anchor when used as the first character of an entire BRE. The implementation may treat the circumflex as an anchor when used as the first character of a subexpression. The circumflex shall anchor the expression (or optionally subexpression) to the beginning of a string; only sequences starting at the first character of a string shall be matched by the BRE. For example, the BRE "^ab" matches "ab" in the string "abcdef", but fails to match in the string "cdefab". The BRE "(^ab)" may match the former string. A portable BRE shall escape a leading circumflex in a subexpression to match a literal circumflex.
美元符号('$'
)在用作整个BRE的最后一个字符时应作为锚点.当用作子表达式的最后一个字符时,该实现可以将美元符号视为锚点.美元符号应将表达式(或子表达式)锚定在要匹配的字符串的末尾;可以说美元符号与最后一个字符后的字符串结尾匹配.
A dollar sign ( '$'
) shall be an anchor when used as the last character of an entire BRE. The implementation may treat a dollar sign as an anchor when used as the last character of a subexpression. The dollar sign shall anchor the expression (or optionally subexpression) to the end of the string being matched; the dollar sign can be said to match the end-of-string following the last character.
由'^'
和'$'
锚定的BRE仅匹配整个字符串.例如,BRE "^ abcdef $"
匹配仅由"abcdef"
组成的字符串.
A BRE anchored by both '^'
and '$'
shall match only an entire string. For example, the BRE "^abcdef$"
matches strings consisting only of "abcdef"
.
这篇关于在Bash正则表达式中,`^`和`$`是指行还是整个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!