Python和正则表达式-替换日期

Python和正则表达式-替换日期,python,regex,Python,Regex,我有多个字符串,格式如下: AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24 我需要从日期开始的月份替换为其他部分,例如: AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24 如何使用python和regex实现这一点 此外,我有多个文本文件,其中包含一行类似于以下内容: LocalTime: 21/4/2019 21:48:41 我需要做与上面相同的事情,用其他东西替换月份。第一个例子: imp

我有多个字符串,格式如下:

AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24
我需要从日期开始的月份替换为其他部分,例如:

AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24
如何使用python和regex实现这一点

此外,我有多个文本文件,其中包含一行类似于以下内容:

LocalTime: 21/4/2019 21:48:41

我需要做与上面相同的事情,用其他东西替换月份。

第一个例子:

import re
string = 'AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24'
replace = '0'+str(int((re.match('(AM-\d{4}-)(\d{2})',string).group(2)))+2)

re.sub('(AM-\d{4}-)(\d{2})',r'\g<1>'+replace,string) #Replace 07 for 07 +2
关于第二个问题:

string2 = 'LocalTime: 21/4/2019 21:48:41'
replace = str(int((re.match(r'(LocalTime: \d{1,2}/)(\d{1,2}).*',string2).group(2)))+2)
re.sub('(Time: \d{2}/)(\d{1,2})',r'\g<1>'+replace,string2) #Replace 4 for 6
如果要限制执行此操作的月份,可以使用If语句:

 if re.match('(AM-\d{4}-)(\d{2})',string).group(2).isin(['04','05','06']:

 if re.match(r'(LocalTime: \d{1,2}/)(\d{1,2}).*',string2).group(2).isin(['4','5','6']:

类似的答案,但有更多的代码和后顾

第一个问题:

import re

#This could be any number of strings, or some other iterable
string_list = []

string_list.append("AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24")
string_list.append("AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24")

#This checks for four digits and a hyphen, then any number of digits to
#replace (which is the month
pattern = r"(?<=\d{4}-)\d+"
#This should be a string
month = "08"
for string in string_list:
    print("BEFORE:  " + string)
    string = re.sub(pattern, month, string)
    print("AFTER:  " + string)
import re

#checks for a colon, 2 numbers, a forward slash, and then selects however many numbers after (which is the month)
pattern = r"/(?<=: \d{2}/)\d+"

#IMO it's better just to write to another file. You can edit the current file you're on, but it's cleaner this way and you won't accidentally screw it up if my regex is wrong.
in_file = open("mytextfile.txt", 'r')
out_file = open("myoutputfile.txt", 'w')
#This should be a string
month = "9"

for line in in_file:
    changed_line = re.sub(pattern, month, line)
    out_file.write(changed_line)

in_file.close()
out_file.close()
第二个问题:

import re

#This could be any number of strings, or some other iterable
string_list = []

string_list.append("AM-2019-04-22 06-47-57865BCBFB-9414907A-4450BB24")
string_list.append("AM-2019-07-22 06-47-57865BCBFB-9414907A-4450BB24")

#This checks for four digits and a hyphen, then any number of digits to
#replace (which is the month
pattern = r"(?<=\d{4}-)\d+"
#This should be a string
month = "08"
for string in string_list:
    print("BEFORE:  " + string)
    string = re.sub(pattern, month, string)
    print("AFTER:  " + string)
import re

#checks for a colon, 2 numbers, a forward slash, and then selects however many numbers after (which is the month)
pattern = r"/(?<=: \d{2}/)\d+"

#IMO it's better just to write to another file. You can edit the current file you're on, but it's cleaner this way and you won't accidentally screw it up if my regex is wrong.
in_file = open("mytextfile.txt", 'r')
out_file = open("myoutputfile.txt", 'w')
#This should be a string
month = "9"

for line in in_file:
    changed_line = re.sub(pattern, month, line)
    out_file.write(changed_line)

in_file.close()
out_file.close()

希望这能有所帮助。

这就是我要找的。是否也可以只替换某个月?我想做的是用07替换06个月,用08替换07个月,依此类推。所以,在1月到5月之间,你不替换,从6月开始,你替换为月份+1?十二月会发生什么?从四月开始到六月结束。我想把四月换成六月,五月换成七月,六月换成八月。请查看我的编辑,应该可以。谢谢你的回复。如果我只想设定某个月的目标,那么模式会是什么?例如,第一个问题为07,第二个问题为7?将d+分别替换为07和7。d+只表示查找任意长度的数字,因此,如果您想将其替换为仅查找07,它将只查找在前面的限定符之后立即有07的字符串。