linux sed 错误,前面的正则表达式无效

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

我正在尝试从空运行阶段的语义释放中解析两个字符串。我创建/发现了一个有效的正则表达式,在https://regex101.com上进行了测试,它有效。当我尝试将其转换为 sed 格式时,它失败了。

尝试了一整天,没有成功。帮助会很棒。

这些是练习弦:

Found git tag v6.0.0 associated with version 6.0.0 on branch main
Found git tag v6.0.0 associated with version 6.0.0-main.2 on branch main
The next release version is 7.0.0
The next release version is 7.0.0-main.12

我想要得到的结果:

6.0.0
6.0.0-main.2
7.0.0
7.0.0-main.12

这是我的正则表达式:

.*(([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([a-zA-Z-[0-9]+\.]*)\+?([a-zA-Z-([0-9]+))?).*

这是我在工作流程中遇到的错误:

Run export PREV_TAG_VERSION=$(npm run semantic-release:dry-run | grep 'associated with version' | sed -E 's/.*((\[0-9\]+)\.(\[0-9\]+)\.(\[0-9\]+)(?:-([a-zA-Z-\[0-9\]+\.]*)\+?([a-zA-Z-(\[0-9\]+))?).*/\1/')
##[debug]/usr/bin/bash -e /home/runner/work/_temp/8b831bec-863f-48d0-84dc-028e0fa78e8f.sh
sed: -e expression #1, char 96: Invalid preceding regular expression
grep: write error: Broken pipe
sed: -e expression #1, char 96: Invalid preceding regular expression
grep: write error: Broken pipe
regex sed grep
1个回答
0
投票

regex101.com 将证明正则表达式可以在 regex101.com 中工作,但这并不意味着它可以在任何其他工具中工作。还要记住这句话

有些人在遇到问题时会想:“我知道,我会用 正则表达式。”现在他们有两个问题。

试试这个:

$ awk '{print (/main$/ ? $(NF-3) : $NF)}' file
6.0.0
6.0.0-main.2
7.0.0
7.0.0-main.12
© www.soinside.com 2019 - 2024. All rights reserved.