Python正则表达式:如何仅增加一个字符串数字?

时间:2019-04-01 18:54:37

标签: python regex

我有以下类型的字符串:

a1 = 'images1subimages1/folder100/hello1.png'
a1 = 'images1subimages1 folder100 hello1.png'
a1 = 'images1subimages1folder100hello1.png'
a1 = 'images1b100d1.png'

字符串的第一个整数是num0,我们只关心它。我们希望将num0的所有出现次数增加一,并保持其他数字不变。

必填:

a2 = 'images2subimages2/folder100/hello2.png'
a2 = 'images2subimages2 folder100 hello2.png'
a2 = 'images2subimages2folder100hello2.png'
a2 = 'images2b100d2.png'

我的尝试

import re
a1 = 'images1subimages1/folder100/hello1.png'

nums = list(map(int, re.findall(r'\d+', a1)))
nums0 = nums[0]
nums_changed = [j+1  if j==nums[0] else j for i,j in enumerate(nums)]
parts = re.findall(r'(\w*\d+)',a1)
for i in range(len(parts)):
  num_parts = list(map(int, re.findall(r'\d+', parts[i])))
  for num_part in num_parts:
    if num_part == nums0:
        parts[i] = parts[i].replace(str(nums0), str(nums0+1))


ans = '/'.join(parts)
ans

结果如下:

a1 = 'images1subimages1/folder100/hello1.png' # good
a1 = 'images1subimages1 folder100 hello1.png' # bad

在python中使用正则表达式有解决问题的一般方法吗?

3 个答案:

答案 0 :(得分:2)

Ì建议先提取第一个数字,然后在没有用re.sub用其他数字括起来的情况下,递增该数字的所有出现次数:

import re
a1 = 'images1subimages1/folder100/hello1.png'
num0_m = re.search(r'\d+', a1)                  # Extract the first chunk of 1+ digits
if num0_m:                                      # If there is a match
    rx = r'(?<!\d){}(?!\d)'.format(num0_m.group())  # Set a regex to match the number when not inside other digits
    print(re.sub(rx, lambda x: str(int(x.group())+1), a1)) # Increment the matched numbers
    # => images2subimages2/folder100/hello2.png

请参见Python demo

答案 1 :(得分:1)

您可以按数字分割字符串,将等于第一个的数字递增,然后重建字符串:

import re


def increment_first(s):
    parts = re.split(r'(\d+)', s)
    nums = list(map(int, parts[1::2]))
    num0 = nums[0]
    nums = [num + (num == num0) for num in nums]
    parts[1::2] = map(str, nums)
    return ''.join(parts)

在您的数据上对其进行测试:

tests = ['images1subimages1/folder100/hello1.png',
'images1subimages1 folder100 hello1.png',
'images1subimages1folder100hello1.png',
'images1b100d1.png']

for test in tests:
    print(test, increment_first(test))

输出:

images1subimages1/folder100/hello1.png images2subimages2/folder100/hello2.png
images1subimages1 folder100 hello1.png images2subimages2 folder100 hello2.png
images1subimages1folder100hello1.png images2subimages2folder100hello2.png
images1b100d1.png images2b100d2.png

答案 2 :(得分:1)

A,我不如这些正则表达式专家那么快。无论如何,这是我的解决方案。

  1. 找到数字re.search(r'\d+', st).group(0)的第一个出现
  2. 用找到的号码之前或之后没有另一个号码(?<!\d)+' + re.escape(first) + r'(?!\d)+的第一次出现代替。
import re


def increment_all_of_first_occurring_number(st):
    first = re.search(r'\d+', st).group(0)
    return re.sub(
        r'(?<!\d)+' + re.escape(first) + r'(?!\d)+',
        str(int(first) + 1),
        st
    )


if __name__ == '__main__':
    a1 = 'images1subimages1/folder100/hello1.png'
    a2 = 'images1subimages1 folder100 hello1.png'
    a3 = 'images1subimages1folder100hello1.png'
    a4 = 'images1b100d1.png'

    b1 = 'images10subimages10/folder10101/hello10.png'
    b2 = 'images10subimages10 folder10101 hello10.png'
    b3 = 'images10subimages10folder10101hello10.png'
    b4 = 'images10b10101d10.png'

    print(increment_all_of_first_occurring_number(a1))
    print(increment_all_of_first_occurring_number(a2))
    print(increment_all_of_first_occurring_number(a3))
    print(increment_all_of_first_occurring_number(a4))

    print(increment_all_of_first_occurring_number(b1))
    print(increment_all_of_first_occurring_number(b2))
    print(increment_all_of_first_occurring_number(b3))
    print(increment_all_of_first_occurring_number(b4))

结果

images2subimages2/folder100/hello2.png
images2subimages2 folder100 hello2.png
images2subimages2folder100hello2.png
images2b100d2.png
images11subimages11/folder10101/hello11.png
images11subimages11 folder10101 hello11.png
images11subimages11folder10101hello11.png
images11b10101d11.png