I am trying to extract number and dots from a string in python3 but with no success. Actually I tried using the regex search function but I get only the first number and not dots and the following number. Here's an example of my test
string = "CDS complement(129..1049)"
print(int(re.search(r'\d+', string).group()))
129
But actually I would like to get:
129..1049
I tried also to improve my code with:
print(int(re.search(r'\d+\.\.\d+', line).group()))
but I get this error:
ValueError: invalid literal for int() with base 10: '129..1049'
Can you help me?
Thanks in advance.
Your regex works fine. re.search(r'\d+\.\.\d+', line)
returns '129..1049'
, and so when you try to pass that string to int()
, it fails. You'll have to manually split
the string into its two integer parts first.
You can use re to get all the numbers:
import re
str1 = "CDS complement(129..1049)"
print(re.findall('\d+', str1))
I am not sure how to get the dots tho
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.