简体   繁体   English

python 正则表达式在新行后删除

[英]python regex remove after new line

I'm trying to drop everything once it sees a CRLF ( \r\n ) line break sequence after the word Total_Cases .一旦在单词Total_Cases之后看到 CRLF ( \r\n ) 换行序列,我就会尝试删除所有内容。

Input 1:输入 1:

Some text\r\nTotal_Cases      -     1     -     1\r\n**Please

Required output 1:需要 output 1:

Some text\r\nTotal_Cases      -     1     -     1`

Input 2:输入 2:

Some text\r\nTotal_Cases     3      1     -     -     -     4\r\n  9 of 15\r\n

Required output:所需 output:

Some text\r\nTotal_Cases     3      1     -     -     -     4

Code used:使用的代码:

re.sub(r'(Total_Cases[\n]).*', r'\1', test1, flags=re.S).rstrip()
re.sub(r'(Total_Cases[\r\n]).*', r'\1', test1, flags=re.S).rstrip()

Neither seems to be working as expected.两者似乎都没有按预期工作。

You may use您可以使用

re.sub(r'(Total_Cases.*?)[\r\n].*', r'\1', test1, flags=re.S).rstrip()

See the Python demo and the regex demo .请参阅Python 演示正则表达式演示

Details细节

  • (Total_Cases.*?) - Group 1 ( \1 from the replacement pattern refers to this value): Total_Cases and then any 0 or more chars, as few as possible (Total_Cases.*?) - 第 1 组(替换模式中的\1指的是这个值): Total_Cases然后是任何 0 个或多个字符,尽可能少
  • [\r\n] - a CR or LF symbol [\r\n] - CR 或 LF 符号
  • .* - any chars up to the string end. .* - 直到字符串结尾的任何字符。

A non-regex approach : iterate over the lines and once the match if found on a line, stop appending lines to the resulting list:非正则表达式方法:遍历行,一旦在行上找到匹配项,停止将行附加到结果列表:

test1 = "More necessary\r\nlines\r\nhere\r\nTotal_Cases - 1 - 1\r\n**Please"
search = "Total_Cases"
lines = test1.splitlines()
results = []
for line in lines:
    results.append(line)
    if search in line:
        break
print ( "\n".join(results) )

Output: Output:

More necessary
lines
here
Total_Cases - 1 - 1

Try this for each string you have as input:为您输入的每个字符串尝试此操作:

input1 =  'Total_Cases - 1 - 1\r\n**Please'
input1.splitlines()[0]

You can try this:你可以试试这个:

x = 'Total_Cases - 1 - 1\r\n**Please'

index = x.index('\r\n')

if index:
    print(x[:index])

else:
    print(x)

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

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