Reputation: 345
I have text like that: "2.3.7-SNAPSHOT-654", Witch means VERSION-SNAPSHOT-BUILD.
so in my case i need two regex, i tried to do this:
[\d\.]
but it's give me all the numbers togohter. can you help me to build those two regex's
Upvotes: 1
Views: 178
Reputation: 3225
Alternative regex for getting digits at the end of a string: "[1-9]+$".
[1-9]
denotes one time any digit[1-9]+
with the "+": any digit at
least one or more times$
denotes the end of the stringIn Python that would be:
import re
text = "2.3.7-SNAPSHOT-654"
regex = re.compile(r"[1-9]+$")
output = re.search(regex, text)
print(output)
#output: <re.Match object; span=(15, 18), match='654'>
Then you can get the rest of the string by slicing the text string:
print(text[:output.regs[0][0]-1])
#output: 2.3.7-SNAPSHOT
Upvotes: 1
Reputation: 626845
You may use the following in Bash:
s="2.3.7-SNAPSHOT-654"
rx='([0-9.]+)-SNAPSHOT-([0-9]+)'
if [[ $s =~ $rx ]]; then
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
fi
See the online demo.
Regex details
([0-9.]+)
- Group 1: one or more digits or dots-SNAPSHOT-
- a literal string([0-9]+)
- Group 2: one or more digits.Upvotes: 1
Reputation: 44
Python:
text = "2.3.7-SNAPSHOT-654"
separator = "-"
text_list = text.split(separator)
number = text_list[-1]
first_part = text.replace(number, "")[:-1]
print(first_part)
print(number)
Or, in short
print("2.3.7-SNAPSHOT-654".split("-")[-1])
print("-".join("2.3.7-SNAPSHOT-654".split("-")[:-1]))
UPD in bash
#!/bin/bash
text="2.3.7-SNAPSHOT-654"
separator="-"
first_part=$(echo "$text" | sed "s/$separator[^$separator]*$//")
number=$(echo "$text" | awk -F "$separator" '{print $NF}')
echo "$first_part"
echo "$number"
Or, in short
echo "2.3.7-SNAPSHOT-654" | sed "s/-[^-]*$//"
echo "2.3.7-SNAPSHOT-654" | awk -F "-" '{print $NF}'
Output:
2.3.7-SNAPSHOT
654
Upvotes: 2
Reputation: 317
Hi I tried this in python:
import re
text = '2.3.7-SNAPSHOT-654'
first_part = re.sub('-[\d]+', '', text)
build_number = re.sub('[\d.]+-\w+-', '', text)
print(first_part)
print(build_number)
Output:
'2.3.7-SNAPSHOT'
'654'
Upvotes: 1