【问题标题】:Regular expression to convert given number in the required format将给定数字转换为所需格式的正则表达式
【发布时间】:2019-06-11 14:34:38
【问题描述】:

我是第一次使用正则表达式,因此需要一个稍微复杂的正则表达式的帮助。我有大约 100-150 个字符串对象(数字)的输入列表。

input = ['90-10-07457', '000480087800784', '001-713-0926', '12-710-8197', '1-345-1715', '9-23-4532', '000200007100272']

Expected output = ['00090-00010-07457', '000480087800784', '00001-00713-00926', '00012-00710-08197', '00001-00345-01715', '00009-00023-04532', '000200007100272']

## I have tried this -

import re
new_list = []
for i in range (0, len(input)):
    new_list.append(re.sub('\d+-\d+-\d+','0000\\1', input[i]))

## problem is with second argument '0000\\1'. I know its wrong but unable to solve
print(new_list)  ## new_list is the expected output.

如您所见,我需要通过添加前导零来将不同格式的数字字符串转换为 15 位数字。

但是这里有一个问题,即一些数字 ie'000480087800784' 已经是 15 位数字,所以应该保持不变(这就是为什么我不能使用 python 的字符串格式(.format)选项)这里必须使用正则表达式,这将只修改所需的数字。我已经尝试过以下答案,但无法解决。

【问题讨论】:

  • 我怀疑 regex way 不是最好的。
  • 嗨@WiktorStribiżew 我尝试了您的解决方案,但没有奏效。你能解释一下我应该如何使用\1
  • 不要使用\1,因为您的正则表达式没有捕获组。如果它不起作用,我认为您没有尝试我的解决方案。使用ideone.com/AGlQLJ
  • 非常感谢@WiktorStribiżew 过去 4 个月我一直在使用 python,但从来没有使用过正则表达式,因此我一直在努力解决它。感谢您的帮助..!
  • 嗨@WiktorStribiżew 是的,它有效。如果您可以在此处发布第二个解决方案,我会将其标记为已接受的答案..!你能不能删除重复的标记 bcz 这不是同一个问题。再次感谢。

标签: python regex python-2.7


【解决方案1】:

您的正则表达式不起作用,因为您在替换中使用了\1,但正则表达式模式没有相应的捕获组。 \1 指的是模式中的第一个捕获组。

如果你想试试正则表达式,你可以使用

re.sub(r'^(\d+)-(\d+)-(\d+)$', lambda x: "{}-{}-{}".format(x.group(1).zfill(5), x.group(2).zfill(5), x.group(3).zfill(5)), input[i])

请参阅Python demo。

这里,^(\d+)-(\d+)-(\d+)$ 匹配一个以 1+ 位开头的字符串,然后是 -,然后是 1+ 位,-,然后是 1+ 位,然后是字符串的结尾。有三个捕获组,它们的值可以通过替换模式中的\1、\2 和\3 反向引用来引用。但是,由于我们需要在每个捕获的文本上应用 .zfill(5),因此使用 lambda 表达式作为替换参数,并通过匹配数据对象 group() 方法访问捕获。

但是,如果您的字符串格式已经正确,您可以根据需要拆分字符串和格式:

for i in range (0, len(input)):
    splits = input[i].split('-')
    if len(splits) == 1:
        new_list.append(input[i])
    else:
        new_list.append("{}-{}-{}".format(splits[0].zfill(5), splits[1].zfill(5), splits[2].zfill(5)))

见another Python demo。两种解决方案都会产生

['00090-00010-07457', '000480087800784', '00001-00713-00926', '00012-00710-08197', '00001-00345-01715', '00009-00023-04532', '000200007100272']

【讨论】:

    【解决方案2】:

    如何分析字符串中的数字和破折号,然后添加前导零?

    input = ['90-10-07457', '000480087800784', '001-713-0926', '12-710-8197', '1-345-1715', '9-23-4532', '000200007100272']
    output = []
    for inp in input:
        # calculate length of string
        inpLen = len(inp)
        # calculate num of dashes
        inpDashes = inp.count('-')
        # add specific number of leading zeros
        zeros = "0" * (15-(inpLen-inpDashes))
        output.append(zeros + inp)
    print (output)
    
    >>> ['00000090-10-07457', '000480087800784', '00000001-713-0926', '00000012-710-8197', '00000001-345-1715', '000000009-23-4532', '000200007100272']
    

    【讨论】:

    • 嗨@Asmox 感谢您的帮助,但这并不是我想要的。但是,你的方法仍然很有趣。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2011-10-02
    • 2016-04-02
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多