本文介绍了巴什正则表达式的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我干了什么错在这里做什么?

试图匹配包含空格,小写字母,大写字母或数字的字符串。特殊字符也会更好,但我认为需要转义某些字符。

  TEST =这是一个测试冠军,有些数字12345和特殊字符*&放大器; ^%$#如果[$ TEST=〜[^ A-ZA-Z0-9 \\]]];然后BLAH;科幻

这显然仅用于上,下,数字和空格测试。虽然不能正常工作。

*更新*

我想我本来应该更具体。这里是code的实际实线。

 如果[$ TITLE=〜[^ A-ZA-Z0-9 \\]]];然后返回=FAIL&放大器;&安培; ERROR =错误:标题只能包含大小写字母,数字和空格!;科幻

*更新*

  ./ anm.sh:行265:在条件前pression语法错误
./anm.sh:行265:语法错误附近`&放大器; *#]
./anm.sh:行265:`如果[! $ TITLE=〜[A-ZA-Z0-9 $%^ \\&放大器; *#]]];然后返回=FAIL&放大器;&安培; ERROR =错误:标题只能包含大小写字母,数字和空格!;返回;科幻


解决方案

有几个重要的事情了解的bash的 [[]] 建设。第一:

The second thing:

Consequently, $v on either side of the =~ will be expanded to the value of that variable, but the result will not be word-split or pathname-expanded. In other words, it's perfectly safe to leave variable expansions unquoted on the left-hand side, but you need to know that variable expansions will happen on the right-hand side.

So if you write: [[ $x =~ [$0-9a-zA-Z] ]], the $0 inside the regex on the right will be expanded before the regex is interpreted, which will probably cause the regex to fail to compile (unless the expansion of $0 ends with a digit or punctuation symbol whose ascii value is less than a digit). If you write [[ $x =~ "[$0-9a-zA-Z]" ]], then the right-hand side will be treated as an ordinary string, not a regex (and $0 will still be expanded). What you really want in this case is [[ $x =~ [\$0-9a-zA-Z] ]]

Similarly, the expression between the [[ and ]] is split into words before the regex is interpreted. So spaces in the regex need to be escaped or quoted. If you wanted to match letters, digits or spaces you could use: [[ $x =~ [0-9a-zA-Z\ ] ]]. Other characters similarly need to be escaped, like #, which would start a comment if not quoted. Of course, you can put the pattern into a variable:

pat="[0-9a-zA-Z ]"
if [[ $x =~ $pat ]]; then ...

For regexes which contain lots of characters which would need to be escaped or quoted to pass through bash's lexer, many people prefer this style. But beware: In this case, you cannot quote the variable expansion:

# This doesn't work:
if [[ $x =~ "$pat" ]]; then ...

Finally, I think what you are trying to do is verify that the variable only contains valid characters. The easiest way to do this check is to make sure that it does not contain an invalid character. In other words, an expression like this:

valid='0-9a-zA-Z $%&#' # add almost whatever else you want to allow to the list
if [[ ! $x =~ [^$valid] ]]; then ...

! negates the test, turning it into a "does not match" operator, and a [^...] regex character class means "any character other than ...".

The combination of parameter expansion and regex operators can make bash regular expression syntax "almost readable", but there are still some gotchas. (Aren't there always?) One is that you could not put ] into $valid, even if $valid were quoted, except at the very beginning. (That's a Posix regex rule: if you want to include ] in a character class, it needs to go at the beginning. - can go at the beginning or the end, so if you need both ] and -, you need to start with ] and end with -, leading to the regex "I know what I'm doing" emoticon: [][-])

这篇关于巴什正则表达式的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:26