Regex 在solaris上获取子字符串的索引
如何找到与solaris10上的正则表达式匹配的子字符串的索引?对于我来说,goto选项是bash、awk和perl。我不确定你想做什么,但这三种方法中的任何一种都可能奏效。例如:Regex 在solaris上获取子字符串的索引,regex,bash,solaris,Regex,Bash,Solaris,如何找到与solaris10上的正则表达式匹配的子字符串的索引?对于我来说,goto选项是bash、awk和perl。我不确定你想做什么,但这三种方法中的任何一种都可能奏效。例如: f=somestring string=$(expr match "$f" '.*\(expression\).*') echo $string $ echo $(match_index "a[0-9][0-9]" "This is a a123 test") 10 对我来说,goto选项是bash、awk和per
f=somestring
string=$(expr match "$f" '.*\(expression\).*')
echo $string
$ echo $(match_index "a[0-9][0-9]" "This is a a123 test")
10
对我来说,goto选项是bash、awk和perl。我不确定你想做什么,但这三种方法中的任何一种都可能奏效。例如:
f=somestring
string=$(expr match "$f" '.*\(expression\).*')
echo $string
$ echo $(match_index "a[0-9][0-9]" "This is a a123 test")
10
您将问题标记为bash,因此我假设您正在询问如何在bash脚本中执行此操作。不幸的是,内置正则表达式匹配不会保存字符串索引。但是,如果为了提取匹配子字符串而询问此问题,那么您很幸运:
if [[ "$var" =~ "$regex" ]]; then
n=${#BASH_REMATCH[*]}
while [[ $i -lt $n ]]
do
echo "capture[$i]: ${BASH_REMATCH[$i]}"
let i++
done
fi
此代码段将依次输出所有子匹配。第一个(索引0)将是整个匹配
不过,您可能更喜欢您的awk
选项。有一个函数match
,它为您提供所需的索引。可以找到文档。如果需要,它还会将匹配的长度存储在RLENGTH
中。要在bash脚本中实现这一点,可以执行以下操作:
match_index=$(echo "$var_to_search" | \
awk '{
where = match($0, '"$regex_to_find"')
if (where)
print where
else
print -1
}')
有很多方法可以处理将变量传递给awk的问题。管道输出和直接嵌入awk one衬里的这种组合相当常见。您还可以使用-v
选项提供awk变量值(请参见man awk
)
显然,您可以修改它以获得长度、匹配字符串,无论您需要什么。如有必要,可以将多个内容捕获到数组变量中:
match_data=($( ... awk '{ ... print where,RLENGTH,match_string ... }'))
您将问题标记为bash,因此我假设您正在询问如何在bash脚本中执行此操作。不幸的是,内置正则表达式匹配不会保存字符串索引。但是,如果为了提取匹配子字符串而询问此问题,那么您很幸运:
if [[ "$var" =~ "$regex" ]]; then
n=${#BASH_REMATCH[*]}
while [[ $i -lt $n ]]
do
echo "capture[$i]: ${BASH_REMATCH[$i]}"
let i++
done
fi
此代码段将依次输出所有子匹配。第一个(索引0)将是整个匹配
不过,您可能更喜欢您的awk
选项。有一个函数match
,它为您提供所需的索引。可以找到文档。如果需要,它还会将匹配的长度存储在RLENGTH
中。要在bash脚本中实现这一点,可以执行以下操作:
match_index=$(echo "$var_to_search" | \
awk '{
where = match($0, '"$regex_to_find"')
if (where)
print where
else
print -1
}')
有很多方法可以处理将变量传递给awk的问题。管道输出和直接嵌入awk one衬里的这种组合相当常见。您还可以使用-v
选项提供awk变量值(请参见man awk
)
显然,您可以修改它以获得长度、匹配字符串,无论您需要什么。如有必要,可以将多个内容捕获到数组变量中:
match_data=($( ... awk '{ ... print where,RLENGTH,match_string ... }'))
假设您希望使用bash查找字符串中通配符的第一个匹配项的位置,则以下bash函数仅返回该位置,如果通配符不匹配,则返回空: function match_index() { local pattern=$1 local string=$2 local result=${string/${pattern}*/} [ ${#result} = ${#string} ] || echo ${#result} } 函数匹配_索引() { 本地模式=$1 本地字符串=$2 本地结果=${string/${pattern}*/} [${#result}=${#string}]| | echo${#result} } 例如:
f=somestring
string=$(expr match "$f" '.*\(expression\).*')
echo $string
$ echo $(match_index "a[0-9][0-9]" "This is a a123 test")
10
$echo$(匹配索引“a[0-9][0-9]”“这是a123测试”)
10
如果希望允许完整的正则表达式而不是通配符,请将“local result=”行替换为
local result=$(echo "$string" | sed 's/'"$pattern"'.*$//')
本地结果=$(回显“$string”| sed的/“$pattern”.*$/”)
但是,您会遇到常见的shell引用问题。假设您想要的是使用bash查找字符串中通配符的第一个匹配位置,则以下bash函数仅返回该位置,如果通配符不匹配,则返回空: function match_index() { local pattern=$1 local string=$2 local result=${string/${pattern}*/} [ ${#result} = ${#string} ] || echo ${#result} } 函数匹配_索引() { 本地模式=$1 本地字符串=$2 本地结果=${string/${pattern}*/} [${#result}=${#string}]| | echo${#result} } 例如:
f=somestring
string=$(expr match "$f" '.*\(expression\).*')
echo $string
$ echo $(match_index "a[0-9][0-9]" "This is a a123 test")
10
$echo$(匹配索引“a[0-9][0-9]”“这是a123测试”)
10
如果希望允许完整的正则表达式而不是通配符,请将“local result=”行替换为
local result=$(echo "$string" | sed 's/'"$pattern"'.*$//')
本地结果=$(回显“$string”| sed的/“$pattern”.*$/”)
但是,您将面临常见的shell引用问题。如果您使用bash4.x,您可以为oobash提供源代码。用bash编写的具有oo风格的字符串库: 字符串是构造函数: 串一个abcda a、 a指数 0 a、 a的最后指数 四, a、 da指数 三, 还有许多“方法”可以在脚本中处理字符串:
-base64Decode -base64Encode -capitalize -center
-charAt -concat -contains -count
-endsWith -equals -equalsIgnoreCase -reverse
-hashCode -indexOf -isAlnum -isAlpha
-isAscii -isDigit -isEmpty -isHexDigit
-isLowerCase -isSpace -isPrintable -isUpperCase
-isVisible -lastIndexOf -length -matches
-replaceAll -replaceFirst -startsWith -substring
-swapCase -toLowerCase -toString -toUpperCase
-trim -zfill
如果您使用bash4.x,那么可以为oobash提供源代码。用bash编写的具有oo风格的字符串库: 字符串是构造函数: 串一个abcda a、 a指数 0 a、 a的最后指数 四, a、 da指数 三, 还有许多“方法”可以在脚本中处理字符串:
-base64Decode -base64Encode -capitalize -center
-charAt -concat -contains -count
-endsWith -equals -equalsIgnoreCase -reverse
-hashCode -indexOf -isAlnum -isAlpha
-isAscii -isDigit -isEmpty -isHexDigit
-isLowerCase -isSpace -isPrintable -isUpperCase
-isVisible -lastIndexOf -length -matches
-replaceAll -replaceFirst -startsWith -substring
-swapCase -toLowerCase -toString -toUpperCase
-trim -zfill