regexp to capture only certain length of characters

3 次查看(过去 30 天)
Hi, I'm trying to capture words with 4 characters only. A word consists or alphanumeric characters.(no symbols of white space)
str4='4-44g 4541 75872 a4cd stuvw';
patt4='(\w{4,4})';
regexp(str4,patt4,'match')
I'm expecting 4541 a4cd as the correct answer. However my code outputs stuv & 7587 in addition. How can I correct this

回答(2 个)

Rashmil Dahanayake
This could be the solution
str4='4-44g 4541 75872 abcd stuvw';
patt4='\<\w{4}\>';
regexp(str4,patt4,'match')

Azzi Abdelmalek
Azzi Abdelmalek 2014-8-7
str4='4-44g 4541 75872 a4cd stuvw';
regexpi([' ' str4 ' '],'(?<=[^0-9,a-z])[0-9,a-z]{4}(?=[^0-9,a-z])','match')

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by