Issue
I want to pick numbers from these strings both integers and decimals with regex in one line command for each.
string="./folder300.5/file300.5"
string2="./folder300/file300"
i want the following output after applying regex for both strings :
300.5
300
Solution
As mentioned in the comment, there's two numbers in each string, but assuming they are always the same as in your examples, you could try:
\d*\.?\d+$
\d* Match multiple numbers
\.? Match dot if present (needed for decimals)
\d+ Match numbers following dot
$ End of string - at least one digit required at the end of your string
You can play with it here.
For example:
import re
string="./folder300.5/file300.5"
string2="./folder300/file300"
regex = "\d*\.?\d+$"
x = re.search(regex, string)
y = re.search(regex, string2)
print(x.group(0)) # 300.5
print(y.group(0)) # 300
Answered By - vs97
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.