从bash数组digit_digit_digit等中删除特定模式

问题描述 投票:0回答:1

我有以下数组

names=( 12_1_3_4_NAME1 1_2_4_NAME2 2_1_name3 1_nAmE4)

我想删除前缀并有

names=( NAME1 NAME2 name3 nAmE4 )

我尝试了一些变体:

names=( "${names[@]/^\d+(?:\_\d+)/}" )
names=( "${names[@]//s*^\d+(?:\_\d+)+\s*/}" )

基于之前的答案:

Use ^ to match the beginning of the string.
Use \d+ to match any number of digits.
Use \. to match a literal . character
Put \.\d+ in a group with () so you can quantify it to match any number of them.
Use re.sub() to replace it with an empty string to remove the match.
Use a raw string so you can put literal backslashes in the regexp without having to escape them.
patt = re.compile(r's*^\d+(?:\.\d+)+\s*')
string = patt.replace('', string

没有成功!我不知道我应该如何在数组中执行它

arrays bash pattern-matching
1个回答
0
投票

这应该有效:

shopt -s extglob
names=("${names[@]//*([[:digit:]])_/}")
© www.soinside.com 2019 - 2024. All rights reserved.