Issue
I am trying to parse two strings from semantic-release from the dry run stage. I created/foudn a Regex that works, tested on https://regex101.com, it works. When I try to convert it to sed format though, it fails.
Tried all day, with no success. Help would be great.
These are the practice strings:
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
The is result I am trying to get:
6.0.0
6.0.0-main.2
7.0.0
7.0.0-main.12
This is the regex I have:
.*(([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([a-zA-Z-[0-9]+\.]*)\+?([a-zA-Z-([0-9]+))?).*
This is the error I am getting during workflow:
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
Solution
Given that you're using sed
to throw away all the non-matching parts of the line, you could just use grep
with the -o
option. Judging from your input data, you could simplify your regex to:
\b([0-9]+\.){2}[0-9]+\S*
which matches:
\b
: a word break([0-9]+\.){2}
: 2 lots of numbers followed by a.
[0-9]+
: some numbers\S*
: 0 or more non-whitespace characters
Regex demo on regex101
Use with grep:
grep -Eo '\b([0-9]+\.){2}[0-9]+\S*' tmp.txt
Output:
6.0.0
6.0.0-main.2
7.0.0
7.0.0-main.12
Or with sed
:
sed -E 's/.*\b(([0-9]+\.){2}[0-9]+\S*).*/\1/' tmp.txt
The output is the same
Answered By - Nick Answer Checked By - Gilberto Lyons (WPSolving Admin)