Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何格式化此python正则表达式?_Python_Regex_Regex Group - Fatal编程技术网

如何格式化此python正则表达式?

如何格式化此python正则表达式?,python,regex,regex-group,Python,Regex,Regex Group,我正在尝试解析文本文件中的数据。数据元组是一个年龄段,后面的0-3次都是“右”对齐的。无论在源数据中跟随一个年龄多少次,我都希望None“pad”三次。年龄和时间都是以空格分隔的,而且时间的格式可以是“mm:ss.dd”或“ss.dd”。年龄和时间可以在一行中重复一次或多次 以下是一些示例数据: test_str = ['25', '24 22.10', '16 59.35 1:02.44', '18 52.78 59.45 1:01.22', '33 59.35

我正在尝试解析文本文件中的数据。数据元组是一个年龄段,后面的0-3次都是“右”对齐的。无论在源数据中跟随一个年龄多少次,我都希望
None
“pad”三次。年龄和时间都是以空格分隔的,而且时间的格式可以是“mm:ss.dd”或“ss.dd”。年龄和时间可以在一行中重复一次或多次

以下是一些示例数据:

test_str = ['25',
    '24 22.10',
    '16 59.35 1:02.44',
    '18 52.78 59.45 1:01.22',
    '33 59.35 1:02.44 34 52.78 59.45 1:01.22 24 25']
扫描后,上述内容应生成元组(或列表、dict等)

我的想法是使用正则表达式,大致如下:

data_search = r'[1-9][0-9]( (([1-9][0-9]:)?[0-9]{2}.[0-9]{2})|){3}'
x = re.search(data_search, test_str[0])
但我没有成功


有人能帮我处理正则表达式或提出更好的解决方案吗?我不确定这是否是最好的方法,但这会分割第一个元素,因为它总是静态地位于第一个位置,然后分割其余元素,并用
None
填补空白

test_str = ['25',
            '24 22.10',
            '16 59.35 1:02.44',
            '18 52.78 59.45 1:01.22']

def create_tuples(string_list):
    all_tuples = []
    for space_string in string_list:
        if not space_string:
            continue
        split_list = space_string.split()
        first_list_element = split_list[0]
        last_list_elements = split_list[1:]
        all_tuples.append([first_list_element] + [None] * (3 - len(last_list_elements)) + last_list_elements)
    return all_tuples

print(create_tuples(test_str))

# Returns:
[['25', None, None, None], ['24', None, None, '22.10'], ['16', None, '59.35', '1:02.44'], ['18', '52.78', '59.45', '1:01.22']]

我不确定这是否是最好的方法,但这会分割第一个元素,因为它总是静态地位于第一个位置,然后分割其余元素,并用
None
填补空白

test_str = ['25',
            '24 22.10',
            '16 59.35 1:02.44',
            '18 52.78 59.45 1:01.22']

def create_tuples(string_list):
    all_tuples = []
    for space_string in string_list:
        if not space_string:
            continue
        split_list = space_string.split()
        first_list_element = split_list[0]
        last_list_elements = split_list[1:]
        all_tuples.append([first_list_element] + [None] * (3 - len(last_list_elements)) + last_list_elements)
    return all_tuples

print(create_tuples(test_str))

# Returns:
[['25', None, None, None], ['24', None, None, '22.10'], ['16', None, '59.35', '1:02.44'], ['18', '52.78', '59.45', '1:01.22']]

我相信这已经接近你想要的了。对不起,缺少正则表达式

def format_str(test_str):
    res = []
    for x in test_str:
        parts = x.split(" ")
        thing = []
        for part in parts:
            if len(thing) != 0 and '.' not in part and ':' not in part:
                res.append(thing[:1] + [None]*(4-len(thing)) + thing[1:])
                thing = [part]
            else:
                thing.append(part)
        if len(thing) != 0:
            res.append(thing[:1] + [None]*(4-len(thing)) + thing[1:])
    return res

test_str = ['25',
    '24 22.10',
    '16 59.35 1:02.44',
    '18 52.78 59.45 1:01.22 24 22.10']

results = format_str(test_str)
print(results)
结果是:

[['25', None, None, None], ['24', None, None, '22.10'], ['16', None, '59.35', '1:02.44'], ['18', '52.78', '59.45', '1:01.22'], ['24', None, None, '22.10']]

我没有在《泰晤士报》上做任何格式化,所以52.78不会显示为0:52.78,但我打赌你可以这样做。如果没有,请留下评论,我也会为此编辑一个解决方案。我相信这已经接近你想要的了。对不起,缺少正则表达式

def format_str(test_str):
    res = []
    for x in test_str:
        parts = x.split(" ")
        thing = []
        for part in parts:
            if len(thing) != 0 and '.' not in part and ':' not in part:
                res.append(thing[:1] + [None]*(4-len(thing)) + thing[1:])
                thing = [part]
            else:
                thing.append(part)
        if len(thing) != 0:
            res.append(thing[:1] + [None]*(4-len(thing)) + thing[1:])
    return res

test_str = ['25',
    '24 22.10',
    '16 59.35 1:02.44',
    '18 52.78 59.45 1:01.22 24 22.10']

results = format_str(test_str)
print(results)
>>> age_expr = r"(\d+)"
>>> time_expr = r"((?:\s+)(?:\d+:)?\d+\.\d+)?"
>>> expr = re.compile(age_expr + time_expr * 3)
>>> [expr.findall(s) for s in test_str]
[[('25', '', '', '')], [('24', ' 22.10', '', '')], [('16', ' 59.35', ' 1:02.44', '')], [('18', ' 52.78', ' 59.45', ' 1:01.22')], [('33', ' 59.35', ' 1:02.44', ''), ('34', ' 52.78', ' 59.45', ' 1:01.22'), ('24', '', '', ''), ('25', '', '', '')]]
结果是:

[['25', None, None, None], ['24', None, None, '22.10'], ['16', None, '59.35', '1:02.44'], ['18', '52.78', '59.45', '1:01.22'], ['24', None, None, '22.10']]

我没有在《泰晤士报》上做任何格式化,所以52.78不会显示为0:52.78,但我打赌你可以这样做。如果没有,请留下评论,我也会为此编辑一个解决方案

我想我可以使用它。如果我能以某种方式沿着“年龄”把绳子分开……我想我可以用它。如果我能以某种方式将字符串沿“年龄”进行初始拆分,那么预期结果中就有一个“(25,无,无)”。这是一个复制错误,对吗?您在预期结果中有一个“(25,无,无)”。这是一个复制错误,对吗?检查冒号是没有必要的:
if len(thing)!=0和“.”不在部分:
就足够了。谢谢我不确定时间是否可以指定为xx:xx而不是xx:xx.xx,因为xx.xx是允许的。很高兴我帮了忙。没有必要检查冒号:
if len(thing)!=0和“.”不在部分:
就足够了。谢谢我不确定时间是否可以指定为xx:xx而不是xx:xx.xx,因为xx.xx是允许的。很高兴我提供了帮助。从那里,您可以迭代结果并修改。我相信这可以用来从与其他不匹配的文本混合了匹配项的文本中提取匹配项。我相信这可以用来从与其他不匹配的文本混合的文本中提取匹配。
>>> age_expr = r"(\d+)"
>>> time_expr = r"((?:\s+)(?:\d+:)?\d+\.\d+)?"
>>> expr = re.compile(age_expr + time_expr * 3)
>>> [expr.findall(s) for s in test_str]
[[('25', '', '', '')], [('24', ' 22.10', '', '')], [('16', ' 59.35', ' 1:02.44', '')], [('18', ' 52.78', ' 59.45', ' 1:01.22')], [('33', ' 59.35', ' 1:02.44', ''), ('34', ' 52.78', ' 59.45', ' 1:01.22'), ('24', '', '', ''), ('25', '', '', '')]]