记事本++Python代码,用于向匹配模式的数字添加值

2024-10-01 00:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个记事本++文件的内容如下

{s:11:"wpseo_title";s:42:"Web Designing training institutes in Kochi";}i:357;a:1:
{s:11:"wpseo_title";s:32:"CSS training institutes in Kochi";}i:358;a:1:
{s:11:"wpseo_title";s:34:"HTML5 training institutes in Kochi";}i:359;a:1:
{s:11:"wpseo_title";s:39:"JavaScript training institutes in Kochi";}i:360;a:1:
{s:11:"wpseo_title";s:32:"XML training institutes in Kochi";}}}

我需要一种方法来搜索短语;s:42:“并将短语的数字部分增加1。在这种情况下,将变成43。在

我只需要这么做。不管它是通过这样一个python脚本实现的

Notepad++ Regular Expression add up numbers

或者其他方法。在

请帮帮我。我对python/任何此类语言都不熟悉。在


Tags: 文件方法inweb内容titletrainingjavascript
3条回答
target_file = "some_file.txt"
with open("tmp_out.txt","w") as f_out:
   with open(target_file) as f_in:
        for line in f_in:
            f_out.write(re.sub(";s:(\d+):",lambda match:";s:%d:"%(int(match.groups(0)[0])+1,line) + "\n")
shutil.move("tmp_out.txt",target_file)

我想是这样的

甚至更好

^{pr2}$

Perl一行程序版本:

perl -ne 's/(?<=;s:)(\d+)(?=:)/$1+1/ge; print' data.txt

以您的链接为例,regex匹配应该是:

def calculate(linenumber, match):
    editor.pyreplace(match.group(0), ';s:%d="%d"' % (match.group(1), str(int(match.group(2))+1)), 0, 0, linenumber, linenumber)

editor.pysearch(r';s:([0-9])="([0-9]+)"', calculate)

我想。我以前从没做过这个!在

相关问题 更多 >