Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to extract consecutive digits of a known length in a string

Subject: how to extract consecutive digits of a known length in a string

From: Ha

Date: 12 Nov, 2012 13:23:10

Message: 1 of 3


E.g.,
s = 'xasdf.123456.dkkk';
s = '342456.xkkd.sdf23.456..dd';

I wanna get the six successive digits from such strings. I guess regular expression can handle this problem but don't know how.

Subject: how to extract consecutive digits of a known length in a string

From: Doug Schwarz

Date: 12 Nov, 2012 17:47:11

Message: 2 of 3

In article <k7qt7u$t34$1@newscl01ah.mathworks.com>,
 "Ha " <scifiles@126.com> wrote:

> E.g.,
> s = 'xasdf.123456.dkkk';
> s = '342456.xkkd.sdf23.456..dd';
>
> I wanna get the six successive digits from such strings. I guess regular
> expression can handle this problem but don't know how.

regexp(s,'\d{6}','match')

will find six digits in a row, but do you want it to fail if there
happen to be seven? That line will find the first six, but you would
have to do something more elaborate to exclude cases of more than six.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: how to extract consecutive digits of a known length in a string

From: Ha

Date: 13 Nov, 2012 05:33:25

Message: 3 of 3

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-088490.12471112112012@news.eternal-september.org>...
> In article <k7qt7u$t34$1@newscl01ah.mathworks.com>,
> "Ha " <scifiles@126.com> wrote:
>
> > E.g.,
> > s = 'xasdf.123456.dkkk';
> > s = '342456.xkkd.sdf23.456..dd';
> >
> > I wanna get the six successive digits from such strings. I guess regular
> > expression can handle this problem but don't know how.
>
> regexp(s,'\d{6}','match')
>
> will find six digits in a row, but do you want it to fail if there
> happen to be seven? That line will find the first six, but you would
> have to do something more elaborate to exclude cases of more than six.
>
> --

Thanks, Doug.

As for my problem, the count of digits is exactly six. So I don't have to worry your worry :-)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us