gpt4 book ai didi

php - 正则表达式匹配电报用户名并在 PHP 中删除整行

转载 作者:行者123 更新时间:2023-12-03 23:04:52 24 4
gpt4 key购买 nike

我想在消息文本中匹配 Telegram 用户名并删除整行,我试过这种模式,但问题是它也匹配电子邮件:.*(@(?=.{5,64}(?:\s|$))(?![_])(?!.*[_]{2})[a-zA-Z0-9_]+(?<![_.])).*模式应该匹配所有这些行:
嗨@username 你好吗?
嗨@username.你好吗?
😉@用户名。
并且不应该像这样匹配电子邮件:
嗨,电子邮件到 something@domain.com

最佳答案

.*\B@(?=\w{5,64}\b)[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*.*
proof \B之前 @意味着在 @ 之前必须有一个非单词字符或字符串的开头.
说明
NODE                     EXPLANATION
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\B the boundary between two word chars (\w)
or two non-word chars (\W)
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\w{5,64} word characters (a-z, A-Z, 0-9, _)
(between 5 and 64 times (matching the
most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to 'Z',
'0' to '9' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
[a-zA-Z0-9]+ any character of: 'a' to 'z', 'A' to
'Z', '0' to '9' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))

关于php - 正则表达式匹配电报用户名并在 PHP 中删除整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63308185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com