2

I need to splite string with file name, in 5 parts, as an example below:

#!/usr/bin/env bash

str="python-zope-proxy-4.3.5-1-x86_64.chi.zst"
pkg_re='(.+)-[^-]+-[0-9]+-([^.]+)\.chi.zst*'

[[ $str =~ $pkg_re ]] &&
   pkg_base = ${BASH_REMATCH[1]}
   pkg_arch = ${BASH_REMATCH[2]}

echo $pkg_base
echo $pkg_arch

The blocks must be as follows:

block 1 - python-zope-proxy # pkg_base
block 2 - 4.3.5-1           # pkg_version_build
block 3 - 4.3.5             # pkg_version
block 4 - 1                 # pkg_build
block 5 - x86_64            # pkg_arch

With the regex below, I get block 1 and block 5, as shown above.

str="python-zope-proxy-4.3.5-1-x86_64.chi.zst"
pkg_re='(.+)-[^-]+-[0-9]+-([^.]+)\.chi.zst*'

[[ $str =~ $pkg_re ]] &&
    pkg_base = ${BASH_REMATCH[1]}
    pkg_arch = ${BASH_REMATCH[2]}

The expression would also have to treat as file name strings in the following cases:

str="python-4.3.5-1-x86_64.chi.zst"
str="python-zope-4.3.5-1-x86_64.chi.zst"
str="python-zope-proxy-4.3.5-1-x86_64.chi.zst"
4
  • I think you can add some extra capturing groups, escape the last dot and remove the asterix after the last t if you just want to match a single t (.+)-(([^-]+)-([0-9]+))-([^.]+)\.chi\.zst regex101.com/r/CSeGSk/1 Commented Nov 1, 2020 at 1:11
  • 1
    @Thefourthbird perfect! Commented Nov 1, 2020 at 1:18
  • @Thefourthbird in some situations the regex doesn't work, where should I post and explain what didn't work? Commented Nov 1, 2020 at 16:10
  • You could add a comment under the answer for which scenario it does not work. Commented Nov 1, 2020 at 16:16

2 Answers 2

3

You could add nested capturing groups to get the values. Note to escape the last dot and remove the asterix after t* as it will match 0+ times a t

(.+)-(([^-]+)-([0-9]+))-([^.]+)\.chi\.zst

See a regex demo and a bash demo

Sign up to request clarification or add additional context in comments.

Comments

3

Leaning more towards the K.I.S.S. principle ... a solution based on parameter expansions:

str="python-zope-proxy-4.3.5-1-x86_64.chi.zst"

block1="${str//-[0-9].*/}"

block2="${str//${block1}-/}"
block2="${block2%-*}"

block3="${block2%%-*}"

block4="${block2#*-}"

block5="${str//${block1}-${block2}-/}"
block5="${block5%%.*}"

echo "block1:${block1}:"
echo "block2:${block2}:"
echo "block3:${block3}:"
echo "block4:${block4}:"
echo "block5:${block5}:"

This generates:

block1:python-zope-proxy:
block2:4.3.5-1:
block3:4.3.5:
block4:1:
block5:x86_64:

Running against the three samples:

for str in "python-4.3.5-1-x86_64.chi.zst" "python-zope-4.3.5-1-x86_64.chi.zst" "python-zope-proxy-4.3.5-1-x86_64.chi.zst"
do
    echo "+++++++++++ ${str}"
    ... block1-block5 processing (copy from above) ...
done

And this generates:

+++++++++++ python-4.3.5-1-x86_64.chi.zst
block1:python:
block2:4.3.5-1:
block3:4.3.5:
block4:1:
block5:x86_64:
+++++++++++ python-zope-4.3.5-1-x86_64.chi.zst
block1:python-zope:
block2:4.3.5-1:
block3:4.3.5:
block4:1:
block5:x86_64:
+++++++++++ python-zope-proxy-4.3.5-1-x86_64.chi.zst
block1:python-zope-proxy:
block2:4.3.5-1:
block3:4.3.5:
block4:1:
block5:x86_64:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.