[英]Python: Use Regular Expression to remove a character from string
The whole file consists of lines like below. 整个文件由如下几行组成。
\"ansText\" : \"11\",
\"boundsX\" : 0,
\"string\" : \"11\"
For any lines starting with title
, I would like to remove the character ;
对于以
title
开头的任何行,我想删除字符;
from the string after it. 从它后面的字符串开始。 Below is an example of expected output
以下是预期输出的示例
Input: 输入:
\"title\" : \"244442424268391(:)7)$(.:$?3.&!&3$83;767:2\",
Output: 输出:
\"title\" : \"244442424268391(:)7)$(.:$?3.&!&3$83767:2\",
I know how to set the regular expression to find the expression title by using: 我知道如何使用以下方式设置正则表达式以查找表达式标题:
str0 = re.sub(r'\"title.*',"\"title\" : ",str0)
But I am not too certain how can I keep the original string but remove only one character. 但是我不太确定如何保留原始字符串,而只删除一个字符。
You can simply do this use str.replace()
and str.startswith()
without RegEx like this: 你可以简单地做这个用
str.replace()
和str.startswith()
没有正则表达式是这样的:
>>> str0 = r'\"title\" : \"244442424268391(:)7)$(.:$?3.&!&3$83;767:2\",'
>>> str0 = str0.replace(';', '') if str0.startswith(r'\"title\" : ') else str0
>>> str0
'\\"title\\" : \\"244442424268391(:)7)$(.:$?3.&!&3$83767:2\\",'
You could use something like so: (\\\\"title\\\\" : \\\\".+?);(.+?\\\\")
(example here ) and replace the string with regex groups number 1 and 2. This expression will look for strings containing \\"title\\"
and a ;
您可以使用类似这样的东西:
(\\\\"title\\\\" : \\\\".+?);(.+?\\\\")
( 此处为示例),并将字符串替换为正则表达式组1和2。将查找包含\\"title\\"
和一个;
字符串;
character within it and use this information to create two regular expression groups, this given \\"title\\" : \\"244442424268391(:)7)$(.:$?3.&!&3$83;767:2\\",
, the output would be:; 字符并使用此信息来创建两个正则表达式组,即给定的
\\"title\\" : \\"244442424268391(:)7)$(.:$?3.&!&3$83;767:2\\",
,,输出将是:
Group 1: \"title\" : \"244442424268391(:)7)$(.:$?3.&!&3$83
Group 2: 767:2\"
When you combine these 2 strings, you will get the result which you are after. 当您组合这两个字符串时,将得到您想要的结果。
str0 = re.sub(r'(\\"title\\" : \\".+?);(.+?\\")',r"\1\2", str0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.