c# - 正则表达式 - 增量替换

标签 c# java php python regex

有没有办法只用正则表达式进行整数增量替换。

这是问题所在,我的文本文件包含 1 000 000 行,全部以 % 开头

我想使用正则表达式逐步将 # 替换为整数。

input:

% line one

% line two

% line three

...

output:

1 line one

2 line two

3 line three

...

最佳答案

n = 1
with open('sourcefile.txt') as input:
    with open('destination.txt', 'w') as output:
        for line in input:
            if line.startswith('%'):
                line = str(n) + line[1:]
                n += 1
            output.write(line)

关于c# - 正则表达式 - 增量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3185669/

相关文章:

c# - 我可以使用 Entity Framework 在单个数据库调用中轻松评估多个 IQueryables 吗?

java - 通过 TreeSet 调用 iterator.next() [来自 Thinking In Java 的示例]

java - 结合 shiro :hasPermission tags

c# - 在内存中加载大量结果集的最佳方法是什么?

c# - Azure Function Apps - 使用登录用户的凭据来读取/更新 Azure 资源

C# - ThreadPool QueueUserWorkItem 使用?

PHP 在 session 过期时运行脚本

java - 用于比较两个 Mat 时 countNonZero 出错

java - Json/Java 新手 - 这是什么数据类型?约会时间? 13 位长。使用 PHP

php - 使用php远程连接到mysql服务器