Regexp exact word match

霸气de小男生 提交于 2021-01-28 04:32:09

问题


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

The four lines are in a text file 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);

回答1:


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');

assuming that l2 is a multiline string.




回答2:


Try this reg exp.

(?:blue bird|yellow card|day)


来源:https://stackoverflow.com/questions/25306738/regexp-exact-word-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!