替换在Python中具有特定模式的行中所有字符串的问题

发布于 2025-01-28 08:12:07 字数 682 浏览 4 评论 0原文

首先,我说我是Python的新手。

line = "lmn = abc(xyz)/123/.../abc(123)"

line = "abc(xyz) = dlf/hmn/abc(fdg)"

我正在尝试的模式替换示例是abc(xxx)= $ xxx与这些行相符。

我创建的正则是(abc \()(。*?)(\)) - >这很好。 现在,如何确保替换发生在一行中的所有位置,因为(。*??)在行的不同位置有所不同。

我发现我们有re.findall(模式,字符串,flags = 0),它将返回一个元组,我可以用来构建表并替换行。 是否有更好的方式可以替代各地的模式?

tmp = re.sub('(abc\\()(.*?)(\\))', '$' **group(1) content**, line , count=???)

上面的问题是我无法在re.sub中使用obj。我称之为

perl,这很简单,一行正则

 =~ s/(abc\\()(.*?)(\\))/(\\$)$2/g

是​​我可以将我指向一个模块或python中的任何python的文档。顺便说一句..我正在使用Python 3.6

Let me start by saying that i am new to python.

line = "lmn = abc(xyz)/123/.../abc(123)"

line = "abc(xyz) = dlf/hmn/abc(fdg)"

The pattern replacement example I am trying is abc(xxx) = $xxx something along these lines.

The regex I have created is (abc\()(.*?)(\)) --> this is working fine.
Now how do I ensure the replacement happens in all the places in a line as the (.*?) is different at different places in a line.

I found that we have re.findall(pattern, string, flags=0) which will return a tuple which I can use to construct a table and go with line replacing.
Is there a better way of substituting the pattern everywhere?

tmp = re.sub('(abc\\()(.*?)(\\))', '

The problem with above is I cant use the obj within the re.sub I call

in perl it was simple one line regex

 =~ s/(abc\\()(.*?)(\\))/(\\$)$2/g

Can you please point me to a document for a module or any regex module in python that I can use for this. Btw..I am using python 3.6

**group(1) content**, line , count=???)

The problem with above is I cant use the obj within the re.sub I call

in perl it was simple one line regex

Can you please point me to a document for a module or any regex module in python that I can use for this. Btw..I am using python 3.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-02-04 08:12:07

您可以在sub的替换模式中使用\<捕获组号来插入捕获组。

因此,如果我正确理解您的问题,这就是您要寻找的:

import re

line1 = "lmn = abc(xyz)/123/.../abc(123)"
line2 = "abc(xyz) = dlf/hmn/abc(fdg)"

# I've simplified the pattern a little to only use two capture groups.
pattern = re.compile(r"(abc\((.*?)\))")

# This is the pattern to replace with: a dollar sign followed by the
# contents of capture group 2.
replacement_pattern = r"$\2"

print(pattern.sub(replacement_pattern, line1)) # lmn = $xyz/123/.../$123
print(pattern.sub(replacement_pattern, line2)) # $xyz = dlf/hmn/$fdg

You can use \<capture group number> in the replacement pattern for sub to insert a capture group.

So if I'm understanding your question correctly, this is what you're looking for:

import re

line1 = "lmn = abc(xyz)/123/.../abc(123)"
line2 = "abc(xyz) = dlf/hmn/abc(fdg)"

# I've simplified the pattern a little to only use two capture groups.
pattern = re.compile(r"(abc\((.*?)\))")

# This is the pattern to replace with: a dollar sign followed by the
# contents of capture group 2.
replacement_pattern = r"$\2"

print(pattern.sub(replacement_pattern, line1)) # lmn = $xyz/123/.../$123
print(pattern.sub(replacement_pattern, line2)) # $xyz = dlf/hmn/$fdg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文