regexp to capture only certain length of characters

조회 수: 2 (최근 30일)
Rashmil Dahanayake
Rashmil Dahanayake 2014년 8월 7일
댓글: Azzi Abdelmalek 2014년 8월 7일
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
Rashmil Dahanayake 2014년 8월 7일
This could be the solution
str4='4-44g 4541 75872 abcd stuvw';
patt4='\<\w{4}\>';
regexp(str4,patt4,'match')
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 7일
This doesn't work for underscore. For example
str4='4-44g 454_ 75872 abcd stuvw'

댓글을 달려면 로그인하십시오.


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