0

给定一个字符串,如:

one/two one/three two/four five/six seven

我使用这个正则表达式:

(?<=\s)([^\/]*)(?=\/|\s)(?!.*\1\b)

要得到:

one
two
five
seven

这就是我想要的结果。所有唯一的“根”字符串。它适用于Rubular,但 bash 不返回任何匹配项。

我知道我正在使用的正则表达式包含一个感叹号,这会混淆 bash,但是在它前面添加一个斜杠转义字符并没有帮助,单引号也没有帮助。

我在 bash 中这样使用它:

[[ $string =~ (?<=\s)([^\/]*)(?=\/|\s)(?!.*\1\b) ]] echo ${BASH_REMATCH}

我不能对正则表达式使用双引号,因为我使用的 bash 版本将双引号中的内容解释为文字字符串。

我怎样才能让 bash 理解这个正则表达式?

4

1 回答 1

2

Bash 绝对不理解 perl 兼容的正则表达式。我会坚持使用 bash 习语:

string="one/two one/three two/four five/six seven"
roots=$(sed 's/\/[^[:blank:]]*//g' <<< "$string" | tr ' ' '\n' | sort -u)
echo "$roots"

或者

roots=()                        # empty array
for word in $string             # no quotes to obtain word splitting
do
    roots+=( ${word%/*} )       # add to the array the bit before the last slash
done
printf "%s\n" "${roots[@]}" | sort -u

或者,使用 bash 4,使用关联数组来模拟集合的行为。

declare -A roots                # an associative array
for word in $string             # no quotes to obtain word splitting
do
    roots[${word%/*}]=1
done
printf "%s\n" "${!roots[@]}"    # print out the hash keys
于 2013-09-25T00:10:56.800 回答