如何在Komodo中使用replace regex?

2024-10-03 21:24:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个正则表达式来查找和替换txt文件中的不同名称(保持为空)。我用Komodo,因为它是考试的一部分。你知道吗

下面是文本示例:

*Cassidy: What's your name again?
Chrissie Watkins: Chrissie!
Cassidy: Where are we going?
Chrissie: Swimming!
Cassidy: Slow up, slow down! I'm not drunk! Slow down! Wait I'm coming! I'm
coming! I'm definitely 
coming! Wait, slow up! I can swim -- just can't walk or dress myself.
Chrissie: Come on in the water!
Cassidy: Take it easy. Take it easy.
Chrissie: Oh! God help me! God! Argh! God help!
Cassidy: I'm coming... I'm coming.
Chrissie: It hurts! It hurts! Oh my god! God help me! God please help!
Martin Brody: How come the sun didn't use to shine in here.
Ellen Brody: We bought the house in the fall, this is summer.
Ellen: Somebody feed the dogs, huh?
Martin: Right.
Ellen: See the kids?
Martin: They must be in the backyard.
Ellen: In Amity, you say: yahd.

必须清除文本,这意味着必须删除每行开头的所有名称(不替换为任何内容)。你知道吗

如果我用这个:\w.¨:

文本中的名字(不是说话的人)也会消失/删除。你知道吗

是否可以只使用一个正则表达式进行清理?你知道吗


Tags: thein文本名称helpdownmartinslow
1条回答
网友
1楼 · 发布于 2024-10-03 21:24:52

你可以用

(?m)^\*?[A-Z][\w' -]*:\s*

参见regex demo

细节

  • (?m)-re.M标志,它使^匹配行的开头
  • ^-行首
  • \*?-可选的*字符
  • [A-Z]-大写字母
  • [\w' -]*-0个或多个单词字符、空格、-或撇号
  • :-冒号
  • \s*-0+空格。你知道吗

相关问题 更多 >