2016-05-12 30 views
0

另一個字符我有一個​​非常簡單的代碼看起來像這樣:確保焦炭的每一次出現之後在bash

txt="hard==2.3.3 soft==2.1.1 weak rep=2.1" 

if [[ $txt =~ ([^\=]\=\=[^\=])* ]]; then 
    echo "Good" 
else 
    echo "Bad" 

# Output is "Bad" 

我想if聲明時,纔會有==是真實的,所以如果有一個獨奏=或者如果有===這是錯誤的。所以基本上,如果有=,那麼後面必須有第二個。如果沒有=(與weak一樣),那就沒問題。

我的代碼存在的問題是,只要txt中至少有一個==if就會成立。有沒有辦法確保每次發生都有第二個=?由於

+0

看起來你需要'([^ =] | ^)=($ | [^ =] )| ==='正則表達式,如果匹配,則返回「Bad」。 –

+0

你缺少一些引號:'txt = hard == 2.3.3 soft == 2.1.1 weak rep = 2.1'會給你'弱:找不到命令' –

回答

1

像你想拒絕箱子的聲音,我的單相等於或三色或更多 - 等於。您目前的if語句接受 double-equals。

所以,你應該這樣做:

txt='hard==2.3.3 soft==2.1.1 weak rep=2.1'; 

function eq { 
    txt="$1"; 
    if [[ ! $txt =~ (^|[^=])=([^=]|$)|={3,} ]]; then 
     echo "Good" 
    else 
     echo "Bad" 
    fi; 
} ## end eq() 

廣泛的測試:

## test one case at a time 
eq 'x '; ## Good 
eq '= '; ## Bad 
eq 'x= '; ## Bad 
eq '=x '; ## Bad 
eq 'x=x '; ## Bad 
eq '== '; ## Good 
eq 'x== '; ## Good 
eq '==x '; ## Good 
eq 'x==x '; ## Good 
eq '=== '; ## Bad 
eq 'x=== '; ## Bad 
eq '===x '; ## Bad 
eq 'x===x'; ## Bad 

## test all pairwise combinations 
eq 'x = '; ## Bad 
eq 'x x= '; ## Bad 
eq 'x =x '; ## Bad 
eq 'x x=x '; ## Bad 
eq 'x == '; ## Good 
eq 'x x== '; ## Good 
eq 'x ==x '; ## Good 
eq 'x x==x '; ## Good 
eq 'x === '; ## Bad 
eq 'x x=== '; ## Bad 
eq 'x ===x '; ## Bad 
eq 'x x===x'; ## Bad 
eq '= x= '; ## Bad 
eq '= =x '; ## Bad 
eq '= x=x '; ## Bad 
eq '= == '; ## Bad 
eq '= x== '; ## Bad 
eq '= ==x '; ## Bad 
eq '= x==x '; ## Bad 
eq '= === '; ## Bad 
eq '= x=== '; ## Bad 
eq '= ===x '; ## Bad 
eq '= x===x'; ## Bad 
eq 'x= =x '; ## Bad 
eq 'x= x=x '; ## Bad 
eq 'x= == '; ## Bad 
eq 'x= x== '; ## Bad 
eq 'x= ==x '; ## Bad 
eq 'x= x==x '; ## Bad 
eq 'x= === '; ## Bad 
eq 'x= x=== '; ## Bad 
eq 'x= ===x '; ## Bad 
eq 'x= x===x'; ## Bad 
eq '=x x=x '; ## Bad 
eq '=x == '; ## Bad 
eq '=x x== '; ## Bad 
eq '=x ==x '; ## Bad 
eq '=x x==x '; ## Bad 
eq '=x === '; ## Bad 
eq '=x x=== '; ## Bad 
eq '=x ===x '; ## Bad 
eq '=x x===x'; ## Bad 
eq 'x=x == '; ## Bad 
eq 'x=x x== '; ## Bad 
eq 'x=x ==x '; ## Bad 
eq 'x=x x==x '; ## Bad 
eq 'x=x === '; ## Bad 
eq 'x=x x=== '; ## Bad 
eq 'x=x ===x '; ## Bad 
eq 'x=x x===x'; ## Bad 
eq '== x== '; ## Good 
eq '== ==x '; ## Good 
eq '== x==x '; ## Good 
eq '== === '; ## Bad 
eq '== x=== '; ## Bad 
eq '== ===x '; ## Bad 
eq '== x===x'; ## Bad 
eq 'x== ==x '; ## Good 
eq 'x== x==x '; ## Good 
eq 'x== === '; ## Bad 
eq 'x== x=== '; ## Bad 
eq 'x== ===x '; ## Bad 
eq 'x== x===x'; ## Bad 
eq '==x x==x '; ## Good 
eq '==x === '; ## Bad 
eq '==x x=== '; ## Bad 
eq '==x ===x '; ## Bad 
eq '==x x===x'; ## Bad 
eq 'x==x === '; ## Bad 
eq 'x==x x=== '; ## Bad 
eq 'x==x ===x '; ## Bad 
eq 'x==x x===x'; ## Bad 
eq '=== x=== '; ## Bad 
eq '=== ===x '; ## Bad 
eq '=== x===x'; ## Bad 
eq 'x=== ===x '; ## Bad 
eq 'x=== x===x'; ## Bad 
eq '===x x===x'; ## Bad 
+0

它完美的工作,謝謝!但我不太明白{3,}到底是什麼意思? – TheGirrafish

+0

「{3,}」是以正則表達式語法綁定的乘數* aka *。它說「三個或更多前面的原子」。在這種情況下,前面的原子是'=',所以'='原子和它的邊界說「三個或更多等於」。根據您的要求,我們必須拒絕任何包含「三個或更多等於」實例的輸入字符串。因此,我們必須對它進行測試(在一個替代組中,因爲還有另一個正則表達式,我們必須拒絕這個正則表達式,這是在先前的交替組中測試的單等於),然後它會被'!'否定拒絕。 – bgoldst

0

考慮一下:

$ cat script.sh 
#!/bin/bash 
txt="a===b" 
re='(^|[^\=])=([^\=]|$)|===' 

for txt in ab a=b a==b a==b a===b a= a== a=== =b ==b ===b 
do 
    if [[ $txt =~ $re ]]; then 
     echo "$txt is Bad" 
    else 
     echo "$txt is Good" 
    fi 
done 

這將產生輸出:

$ bash script.sh 
ab is Good 
a=b is Bad 
a==b is Good 
a==b is Good 
a===b is Bad 
a= is Bad 
a== is Good 
a=== is Bad 
=b is Bad 
==b is Good 
===b is Bad