简体   繁体   English

在使用Regex的python中,如何在带有短括号字符的字符串之后插入字符串?

[英]In python using Regex, how do I insert a string after a string with a close-parenthesis character?

I have lines of data like this: 我有这样的数据行:

(ABCD:0.00825830327156463345,(LKSDJF:0.00000254996576400768,SDFADS:0.00917039554517301569):0.16367666117488463562,OIUO:0.00401845774067355072):0.0;
((OIUO:0.00298782818816040099,SDFADS:0.00148583412998809050):0.27857371651571366522,ABCD:0.00188545323182991121,LKSDJF:0.00799482946501124843):0.0;

And I am trying to use regex to insert a string after every every "):randomfloatingpoint_number". 而且我正在尝试使用正则表达式在每个“):randomfloatingpoint_number”之后插入一个字符串。 Sometimes the number is following by a comma and sometimes by a semicolon. 有时数字后跟逗号,有时后跟分号。

This is the code I wrote: 这是我写的代码:

        line = re.sub(r"):[0-9\.])",r"\1 " + string, line)

But I get an error message that the parenthesis are unbalanced: 但是我收到一条错误消息,指出括号不平衡:

Traceback (most recent call last):
File "AssignFGbranches.py", line 31, in <module>
line = re.sub(r"):[0-9\.])",r"\1 " + string, line)
File "/usr/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/usr/lib/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

You need to escape the brackets. 您需要逃脱括号。 Also, there is no number that is immediately followed by a closing brace. 另外,没有数字后紧跟一个右花括号。 You may want something like this: 您可能想要这样的东西:

(\):[0-9.]+[;,])

This will match the following: 这将匹配以下内容:

554517301569 ):0.16367666117488463562, OIUO 554517301569 ):0.16367666117488463562, OIUO

There are two issues. 有两个问题。 One is that the literal close parens needs to be escaped. 一是字面量接近的括号需要被转义。 The other is that the open parens for the group was missing: 另一个是缺少该组的开放式括号:

re.sub(r"(\):[0-9\.]+)",r"\1 " + string, line1)

暂无
暂无

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

相关问题 在 python 中使用正则表达式在冒号或括号后提取字符串 - Extract string after colon or parenthesis with regex in python Python 正则表达式 - 如何使用 python 正则表达式获取字符串中特定单词后的单词? - Python Regex - How do I fetch a word after a specific word in a string using python regex? Python和正则表达式:用括号分割字符串 - Python and regex: split a string with parenthesis 如何在 Python 中使用正则表达式按括号拆分列表中的值? - How do I split values in a list by parenthesis using regex in Python? 不获取字符串的第一个字符并使用正则表达式获取括号中的数据 - Not getting the first character of the string and get the data which is in a parenthesis using regex pandas/regex: 去掉连字符或括号字符后的字符串(包括) carry string in the comma after the comma in pandas dataframe - pandas/regex: Remove the string after the hyphen or parenthesis character (including) carry string after the comma in pandas dataframe 使用正则表达式拆分 aa 字符串中的括号 - using a regex to split the parenthesis in a a string 如何使用 python 在字符串中一定数量的字符后插入空格? - How do I insert a space after a certain amount of characters in a string using python? 如何使用正则表达式在出现字符串后替换特定字符 - How to replace a specific character after the occurrence of a string using regex Python - 如何在每第 n 个字符后将“*”插入字符串 - Python - How to insert '*' into a string after every nth character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM