正则表达式完全匹配 [英] Regexp exact word match
本文介绍了正则表达式完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要匹配行中的单词.例如:
I need to match words from lines. For example:
The blue bird is dancing.
Yellow card is drawn
The day is perfect rainy
blue bird is eating
这四行在文本文件 l2
中.
我想匹配蓝鸟,黄牌,日期,并且每次打印一行时,都在该行之前打印匹配的单词.
I want to match the blue bird, yellow card, day and every time a line is printed that matched word is printed before the line.
y=regexp(l2,('^(?=.*blue bird)|(?=.*day)|(?=.*Yellow card)$'));
这是如何工作的?我无法得到结果.
Is this how it works? I can't get the result.
sprintf('[%s]',y,l2);
推荐答案
MATLAB's regex engine doesn't use \b
as word boundary anchors but \<
and \>
.
所以你的正则表达式会变成
So your regex would become
y = regexp(l2, '^(?=.*\<(?:blue bird|day|Yellow card)\>).*', 'lineanchors');
假设 l2
是多行字符串.
这篇关于正则表达式完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文