【问题标题】:Extract conversations using regex使用正则表达式提取对话
【发布时间】:2019-04-29 15:04:05
【问题描述】:

我有这样的文字:

[agent]:欢迎来到ABC银行我的名字是Asif。请问有什么可以帮你 [cust]:我的信用卡有额外费用,我不会支付,请删除 [代理]:好的,我可以保持通话吗 [cust]:这在银行方面是非常迟钝的行为

对话不是行分隔的。我只需要提取客户所说的内容,而忽略代理所说的分析客户情绪。请帮助解决这个正则表达式。

【问题讨论】:

  • "Give me a regex that does X" 而没有尝试的问题在 Stack Overflow 上是题外话。
  • 我认为@MagicBeans 的意思是“我已经尝试过\]:.*,但它不起作用。”

标签: python regex text


【解决方案1】:

要么:

\[cust\]:((?:(?!\[\w+\]:).)*)

(?s)\[cust\]:(.*?)(?=\[\w+\]:|$)

https://regex101.com/r/RT2O4y/1

基准测试:

Regex1:   \[cust\]:((?:(?!\[\w+\]:).)*)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   2
Elapsed Time:    1.37 s,   1372.69 ms,   1372693 µs
Matches per sec:   72,849


Regex2:   (?s)\[cust\]:(.*?)(?=\[\w+\]:|$)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   2
Elapsed Time:    0.92 s,   918.17 ms,   918175 µs
Matches per sec:   108,911

【讨论】:

    猜你喜欢
    • 2013-01-04
    • 2014-01-27
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多