- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
想知道为什么以下内容对角色不起作用:+
字符 "\"、"(" 和 "*" 是有意义的,即 * 将扩展为当前目录中的文件夹/文件(在命令行 shell 扩展期间)以及类似的 \ 和 ( 将期望结束字符起作用,但我的理解是 "+" 应该像 "-" 那样工作。
PS:我知道在 IF 语句中放置双引号,即“${o}”,将适用于我下面的测试用例中的所有字符。在带或不带双引号的 IF 语句中使用\${o} 将导致所有检查失败。
$ for o in - + \` ~ \~ , _ = / \\ ! @ \# $ \$ % ^ \& \* \( \); do a="a${o}b${o}c";if [[ $a =~ ${o} ]]; then echo "${o} exists in $a and =~ works"; else echo -e "\ncharacter ${o} doesn't work with =~\n"; fi; done
- exists in a-b-c and =~ works
character + doesn't work with =~
` exists in a`b`c and =~ works
/home/ubuntu exists in a/home/ubuntub/home/ubuntuc and =~ works
~ exists in a~b~c and =~ works
, exists in a,b,c and =~ works
_ exists in a_b_c and =~ works
= exists in a=b=c and =~ works
/ exists in a/b/c and =~ works
character \ doesn't work with =~
! exists in a!b!c and =~ works
@ exists in a@b@c and =~ works
# exists in a#b#c and =~ works
$ exists in a$b$c and =~ works
$ exists in a$b$c and =~ works
% exists in a%b%c and =~ works
^ exists in a^b^c and =~ works
& exists in a&b&c and =~ works
character * doesn't work with =~
character ( doesn't work with =~
) exists in a)b)c and =~ works
最佳答案
这个问题背后的根本误解是 =~
是一个子字符串搜索运算符。 不是。
=~
的右侧被评估为 POSIX ERE 表达式。 =~
因此是一个正则表达式匹配运算符,当右侧被引用以使其内容成为文字时(或者当已知该字符串仅匹配自身时,它恰好经常用于搜索解释为 ERE)。
+
在正则表达式中表示“1 个或多个前面的标记”——正如 *
表示“0 个或多个前面的标记” ".
因此,[[ $foo =~ + ]]
或 [[ $foo =~ * ]]
都没有意义,因为它们正在检查零或-多个根本不存在的前置标记。
类似地,(
和 )
在 ERE 中作为匹配组的开始和结束具有意义,因此当它们被给出时(未转义/未引用),它们导致无效的正则表达式。
如果您引用扩展,相比之下,包含的所有字符都将被视为文字,而不是被视为正则表达式元字符,从而导致预期的行为。
如果你想检查一个文字字符是否包含在一个字符串中,要么引用它 -- [[ $foo =~ "$o"]]
-- 或者使用 glob-style模式:[[ $foo = *"$o"* ]]
关于linux - BASH =~ 包含子串 : "+" or a regex character behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40748821/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!