As an alternative to the solution provided by the user @Marcelovismari, here is a regular expression compatible with bash
4.2+:
str="python-zope-proxy-4.3.5-1-x86_64.chi.zst"
if [[ $str =~ ([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)-(([0-9]+(\.[0-9]+)*)(-([0-9]+))?)-([^.]+).* ]]; then
for group_num in "${!BASH_REMATCH[@]}"; do
echo "group ${group_num}: ${BASH_REMATCH[$group_num]}"
done
fi
Whose output is:
group 0: python-zope-proxy-4.3.5-1-x86_64.chi.zst
group 1: python-zope-proxy
group 2: -proxy
group 3: 4.3.5-1
group 4: 4.3.5
group 5: .5
group 6: -1
group 7: 1
group 8: x86_64
The interest groups in this example would be the 1
, 3
, 4
, 7
and 8
.
EDIT 1: Modified quantifier +
(1 or more characters) to *
(0 or more characters) in suffix of pkg_base
(first -
onwards after python
, in the example) and pkg_version
/pkg_version_build
(first .
onwards after 4
, in the example).
EDIT 2: Unlocking the regex proposal:
([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*)
- Root group capture starting with a string 1 characters or more ([a-zA-Z0-9]+
) and ending with an optional subgroup ((-[a-zA-Z0-9]+)*
) starting with a hyphen and followed by a string 1 characters or more
That should correspond to python-zope-proxy
, python-zope-proxy-lorem-ipsum-dolor...
(tending to infinity) or just python
(([0-9]+(\.[0-9]+)*)(-([0-9]+))?)
- Root group capture starting with a subgroup containing an integer number of 1 or more digits ([0-9]+
), an optional nested subgroup ((\.[0-9]+)*
) starting with a point (\.
) and followed by an integer number of 1 or more digits; closed the first subgroup, a new optional subgroup begins ((-([0-9]+))?
, with the quantifier ?
indicating 0 or 1 occurrences) started by a hyphen and followed by a nested subgroup containing an integer of 1 or more digits.
That should correspond to 4.3.5
, 4.3.5-1
, 4
or 4-1
. Taking 4.3.5-1
as an example, this part of the regular expression would be grouping together 4.3.5-1
, 4.3.5
and 1
in dedicated subgroups (the 2 subgroups adjacent to the root)
([^.]+)
- Root group capture starting with a lista negada
and capturing any 1 or more characters.
This should match any size and type of characters until it stops in the character .
(or at the end of string); p ex.. x86_64
(x86_64.chi.zst
), x86
(x86.zst
), x64
(x64.chi
), x
(x
), etc..
EDIT 3: Added output code enforcement bash
presented in the solution.
EDIT 4: As @hkotsubo mentioned in her comment, both quantifiers *
as +
are classified as Greedy; quantifiers non-greedy or greedy - among other information relevant to quantifiers in general - are described in more detail in their answers to the questions Difference between unreasonable quantifiers ?? and *? and Regular Expressions: Lazy quantifier function "?".
Try to use the expression
([a-zA-Z0-9]+(-[a-zA-Z0-9]+){,2})-(([0-9]+(\.[0-9]+){,2})((\-)([0-9]+))?)-([a-zA-Z0-9]+_[a-zA-Z0-9]+).*
and the indices1
,3
,4
,8
and9
of arrayBASH_REMATCH
, respectively.– Rfroes87
@Rfroes87 Your code worked perfectly, thank you
– vcatafesta
Great! When possible, I ask you to check both answers (mine and @Marcelovismari) and accept the answer that best suits what you need, please.
– Rfroes87
Important in these cases to say what you tried (a [mcve] preferably) and the difficulty found. To better enjoy the site, understand and avoid closures and negativations worth reading What is the Stack Overflow and then the Stack Overflow Survival Guide (summarized) in Portuguese.
– Bacco
@Bacco, I disagree on the mistaken closing, claiming lack of examples; both the question and the answers were clear, so much so that it was understood from first and posted answer even with examples.
– vcatafesta
@vcatafesta was not mistaken no, if it were the community would have voted to reopen then. The system is designed for this. Maybe after editing (that the community has access to the complete history) the community can reconsider, but still remains to explain what tried to solve and failed. Anyway, if you study the links I passed maybe better understand the purpose of the site and the reason for the closure.
– Bacco
The title could also be better worked.
– Rfroes87
P.ex. How to group substrings in condition and return them with BASH_REMATCH or something like that.
– Rfroes87
@Bacco thank you for the explanation, sincerely thank you, in order to be the first or perhaps the second publication, in the future I will take care to follow the rules.
– vcatafesta
@Rfroes87, I will accept the suggestion regarding the title, anyway thank you to all.
– vcatafesta