给定一个字母数字字符串序列,输出包含至少一个字母和一个数字的最大字符串的长度。如果不存在这样的字符串,则输出 0。
应该使用 Bash
答案 0 :(得分:1)
使用 GNU awk:
awk 'BEGIN { map[0]="" } { for(i=1;i<=NF;i++) { if ($i ~ /[[:digit:]]/ && $i ~ /[[:alpha:]]/) { map[length($i)]=$i } } } END { PROCINFO["sorted_in"]="@ind_num_asc";for (i in map) { len=i } print len}' <<< "this sfdsf fdgdf 6gdfgg 56gdfggf67dfgdg"
说明:
awk 'BEGIN {
map[0]="" # Initialise an array map with a 0 string length index
}
{
for(i=1;i<=NF;i++) { # Loop through each work in the sequence of string
if ($i ~ /[[:digit:]]/ && $i ~ /[[:alpha:]]/) {
map[length($i)]=$i # If the word matches a digit and it matches a alpha character, add to the map array with the length of the word as the index and the word the value
}
}
}
END {
PROCINFO["sorted_in"]="@ind_num_asc"; # At the end of processing the sequence, set the array ordering
for (i in map) {
len=i # Loop through the array setting len to the index
}
print len # Print len, (the highest length)
}' <<< "this sfdsf fdgdf gdfgg 56gdfggf67dfgdg"