我不想使用字符串拆分,因为我有数字1-99,并且在文本中某处包含'#/#'的字符串列。
在以下示例中,如何编写正则表达式来提取数字10:
He got 10/19 questions right.
答案 0 :(得分:1)
答案 1 :(得分:1)
如果您仔细构造str.split()
,您仍然可以使用它:
t = "He got 10/19 questions right."
t2 = "He/she got 10/19 questions right"
for q in [t,t2]:
# split whole string at spaces
# split each part at /
# only keep parts that contain / but not at 1st position and only consists
# out of numbers elsewise
numbers = [x.split("/") for x in q.split()
if "/" in x and all(c in "0123456789/" for c in x)
and not x.startswith("/")]
if numbers:
print(numbers[0][0])
输出:
10
10
答案 2 :(得分:0)
res = re.search('(\d+)/\d+', r'He got 10/19 questions right.')
res.groups()
('10',)
答案 3 :(得分:0)
import re
myString = "He got 10/19 questions right."
oldnumber = re.findall('[0-9]+/', myString) #find one or more digits followed by a slash.
newNumber = oldnumber[0].replace("/","") #get rid of the slash.
print(newNumber)
>>>10
答案 4 :(得分:0)
查找正斜杠之前的所有数字,并使用起止括号将正斜杠排除在外。
>>> import re
>>> myString = 'He got 10/19 questions right.'
>>> stringNumber = re.findall('([0-9]+)/', myString)
>>> stringNumber
['10']
这将返回所有以正斜杠结尾但在字符串列表中的数字。如果需要整数,则应使用map
int
列表,然后再次创建list
。
>>> intNumber = list(map(int, stringNumber))
>>> intNumber
[10]