regex - 匹配以@开头的单词
问题描述
我有下一个模式(^| )@[^@ ]+($|)
我有下一个字符串:cdc@cd csd@ @as @refactoringGuru @ @as@refactoringGuru
.
所以,在这种情况下,我只匹配一个单词——@as
单词前有空格。
但我也需要数学@refactoringGuru
。
所以,我需要下一个输出 -@as
@refactoringGuru
单词前没有空格。我不匹配@as@refactoringGuru
我怎么能做到这一点?
解决方案
您可以尝试使用否定的lookbehind(?<!\S)@
来匹配@
单词开头的符号:
(?<!\S)@\w+(?!\S)
演示
正则表达式的解释:
(?<!\S) assert that what precedes is either whitespace or the start of the input
@ match an @ symbol
\w+ match one or more word characters
(?!\S) assert that what follows is either whitespace or the end of the input
推荐阅读
- amazon-web-services - 在 Lambda 触发器上从 S3 逐行读取文件
- python - 为什么我在 pandas 中的 python 函数没有返回任何值?
- c# - 在循环内运行时添加实体的 EF 核心错误
- azure - 外部表 Azure Synapse 不返回数据
- python - Catkin_make 不更新我保存的 python 文件
- postgresql - 尽管有索引,但在 postgres 表上搜索速度很慢
- python - 如何根据 Pandas 中的条件将缺少的行从一个数据帧添加到另一个数据帧?
- azure - Azure 服务总线:删除会话消息
- postgresql - 高效地在 postgresql 中循环更新记录
- android - 在 IOS 上从 Flutter 启动 Whatsapp 时出现问题