我需要从变量中删除带有特殊字符的字符串
我有这个字符串,这是一条路径,我想从一个存储在变量中的更大路径中删除。
所以这些是参数:
FULL_PATH=Server/.*/resources/schema/v12_55_6/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_6/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_6/.*/.*.java,Server/.*/resources/indexes/v12_55_6/.*.index,Server/.*/resources/schema/v12_55_7/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_7/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_7/.*/.*.java,Server/.*/resources/indexes/v12_55_7/.*.index,Server/.*/resources/schema/v12_55_8/.*/.*-dbSchemaDescriptor.xml,Server/.*/resources/SpringIOC/dataupgrader/v12_55_8/.*/.*-dataUpgrader.xml,Server/.*/java/com/company/mqm/dataupgrader/v12_55_8/.*/.*.java,Server/.*/resources/indexes/v12_55_8/.*.index
这是我想要从变量中删除的路径(让我们假设它是一个字符串):
REMOVE_PATH=Server/.*/resources/SpringIOC/dataupgrader/12_55_7/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/mqm/dataupgrader/12_55_7,Server/.*/resources/indexes/12_55_7/.*\.index/.*/.*\.java
我尝试使用shopt但它没有做任何事情, 这是我使用的命令:
shopt -s extglob;echo ${FULL_PATH//@($"{REMOVE_PATH}")}
* FULL_PATH变量是动态的,可以改变,所以我把它放在参数中。
答案 0 :(得分:2)
了解模式替换如何与shopt exglob一起使用的简单示例:
Import-Module IISAdministration
$ConfigSection = Get-IISConfigSection -SectionPath "system.webServer/security/access" -Location "MyWebSite"
#to set:
Set-IISConfigAttributeValue -AttributeName sslFlags -AttributeValue Ssl -ConfigElement $ConfigSection
#to read:
Get-IISConfigAttributeValue -ConfigElement $ConfigSection -AttributeName sslFlags
请注意,模式中具有特殊含义的字符可以使用反斜杠shopt -s extglob
full_path=a/b,a/c,a/d,a/e,a/f
remove_path=a/d,a/c
echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/b,a/e,a/f
# a/d and a/c where removed
# ?(,) at the end to remove comma after match if any
full_path=a/*/b,a/*/c,a/*/d,a/*/e,a/*/f
remove_path=a/*/d
echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/*/e,a/*/f
# here a/*/b was removed because in a/*/d * matches every character including / so a/*/d longest match is a/*/b,a/*/c,a/*/d
\
否则匹配除full_path='a/*/b,a/*/c,a/*/d,a/*/e,a/*/f'
remove_path='a/\*/d,a/\*/c'
echo "${full_path//@(${remove_path//,/|})?(,)}"
# a/*/b,a/*/e,a/*/f
以外的所有字符,例如可以使用以下模式/
:除*([^/])
以外的0个或更多字符
/