简体   繁体   English

找到不确定的数字和破折号序列正则表达式python

[英]finding an indefinite sequence of numbers and dashes regex python

So I have a bunch of strings that contain a sequence of numbers and dashes:所以我有一堆包含数字和破折号序列的字符串:

strings = [
     '32sdjhsdjhsdjb20-11-3kjddjsdsdj435',
     'jdhjhdahj200-19-39-2-12-2jksjfkfjkdf3345',
     '1232sdsjsdkjsop99-7-21sdjsdjsdj',
]

I have a function:我有一个功能:

def get_nums():
    for string in strings:
        print(re.findall('\d+-\d+', string))

I want this function to return the following:我希望这个函数返回以下内容:

['20-11-3']
['200-19-39-2-12-2']
['99-7-21']

But my function returns:但我的函数返回:

['20-11']
['200-19', '39-2', '12-2']
['99-7']

I have no idea how to return the full sequence of numbers and dashes.我不知道如何返回完整的数字和破折号序列。

The sequences will always begin and end with numbers, never dashes.序列总是以数字开始和结束,而不是破折号。 If there are no dashes between the numbers they should not be returned.如果数字之间没有破折号,则不应返回它们。

How can I use regex to return these sequences?如何使用正则表达式返回这些序列? Is there an alternative to regex that would be better here?有没有比正则表达式更好的替代方法?

def get_nums():
    for string in strings:
        print(re.findall('\d+(?:-\d+)+', string))

This needs to be (?:…) rather than just (…) , see https://medium.com/@yeukhon/non-capturing-group-in-pythons-regular-expression-75c4a828a9eb这需要是(?:…)而不仅仅是(…) ,请参阅https://medium.com/@yeukhon/non-capturing-group-in-pythons-regular-expression-75c4a828a9eb

import re

strings = [
    '32sdjhsdjhsdjb20-11-3kjddjsdsdj435',
    'jdhjhdahj200-19-39-2-12-2jksjfkfjkdf3345',
    '1232sdsjsdkjsop99-7-21sdjsdjsdj',
]

def get_nums():
    for string in strings:
        print(re.search(r'\d+(-\d+)+', string).group(0))

get_nums()

Output:输出:

20-11-3  
200-19-39-2-12-2  
99-7-21  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM