简体   繁体   English

正则表达式在 Python 中替换选择特定的子字符串

[英]Regex replace in Python picking a specific substring

Here's what I want to happen:这是我想要发生的事情:

input = "asdsad,200200-12964,0009,""TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589,037.17"" 0.00000000,1.000000"

output = "asdsad,200200-12964,0009,""TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589.037.17"" 0.00000000,1.000000"

How can I change the comma ( , ) to a dot ( . ) between ""...589,037.17..."" in Python using regex.如何使用正则表达式在 Python 中的""...589,037.17...""之间将逗号 ( , ) 更改为点 ( . )。

Extra: 589,037.17 => 589.037.17

I tried:我试过:

print(re.sub(r'(?<=\d),', '.', input))

But my output was:但我的输出是:

output = "asdsad,200200-12964.0009,""TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589.037.17"" 0.00000000,1.000000"

First, don't call a variable input , because it overwrites the the built-in function input() .首先,不要调用变量input ,因为它会覆盖内置函数input() Also you repeated strings are just one string in Python.此外,您重复的字符串只是 Python 中的一个字符串。

i = 'asdsad,200200-12964,0009,TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589,037.17 0.00000000,1.000000'

To solve your specific case, you could match a the country code followed by 3 numbers in the first bit of the price before the comma.为了解决您的具体情况,您可以在逗号前的价格的第一位匹配国家代码后跟 3 个数字。 That works for this, but probably isn't generic enough for any country code and any price, as look-behinds must be of fixed width.这适用于此,但对于任何国家/地区代码和任何价格可能都不够通用,因为后视必须具有固定宽度。

print(re.sub(r'(?<=USD \d{3}),', '.', i))

I would use a look-behind for the country code and space, then group the first bit of the number and replace it with a backreference.我会对国家代码和空格使用后视,然后将数字的第一位分组并用反向引用替换它。

print(re.sub(r'(?<=[A-Z]{3} )(\d+),', r'\1.', i))
import re

input = "asdsad,200200-12964,0009,""TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589,037.17"" 0.00000000,1.000000"
print(input)
print(re.sub(r'USD (\d+),(\d+)', r'USD \1.\2', input))

Output:输出:

asdsad,200200-12964,0009,TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589,037.17 0.00000000,1.000000
asdsad,200200-12964,0009,TREASURY SETTLEMENT NON-COMPLIANCE ASSESSMENT FOR CPD2020-01-21 USD 589.037.17 0.00000000,1.000000

You can go through this Search and Replace andthis link for documenation on this.您可以通过此 搜索和替换以及链接获取有关此的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM