且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ksh中的变量上进行精确的单词匹配?

更新时间:2022-05-05 23:45:33

测试行是否在单词分隔字符之间包含单词.一个有用的技巧是在字符串的开头和结尾添加一个以单词分隔的字符,以防单词位于字符串的开头或结尾.

Test whether the line contains the word between word-separating characters. A helpful trick is to add a word-separating character at the beginning and at the end of the string, in case the word is at the beginning or end of the string.

[[ " $LINE_FROM_FILE " == *[![:alnum:]]Validation[![:alnum:]]* ]]

这假定单词仅由字母和数字组成.如果定义不同,请调整图案.

This assumes that words consist of letters and digits only. Adjust the pattern if you have a different definition.

请注意,您编写的测试[[ "${LINE_FROM_FILE##*Validation}" != "${LINE_FROM_FILE}" ]]是编写[[ $LINE_FROM_FILE = *Validation* ]]的一种复杂方法(即,将Validation作为子字符串进行检查).

Note that the test you wrote, [[ "${LINE_FROM_FILE##*Validation}" != "${LINE_FROM_FILE}" ]], is a complicated way of writing [[ $LINE_FROM_FILE = *Validation* ]] (i.e., checking for Validation as a substring).