问题
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