写一个正则表达式来提取'/'之前的数字

时间:2019-03-27 22:25:52

标签: python regex

我不想使用字符串拆分,因为我有数字1-99,并且在文本中某处包含'#/#'的字符串列。

在以下示例中,如何编写正则表达式来提取数字10:

He got 10/19 questions right.

5 个答案:

答案 0 :(得分:1)

使用前瞻性在/上进行匹配,如下所示:

\d+(?=/)

如果您的实现将/作为分隔符,则可能需要转义。

实时示例:https://regex101.com/r/xdT4vq/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]