Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

3 views
Skip to first unread message

Ha

unread,
Nov 12, 2012, 8:23:10 AM11/12/12
to

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.

Doug Schwarz

unread,
Nov 12, 2012, 12:47:11 PM11/12/12
to
In article <k7qt7u$t34$1...@newscl01ah.mathworks.com>,
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.

Ha

unread,
Nov 13, 2012, 12:33:25 AM11/13/12
to
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-088490.1...@news.eternal-september.org>...
> In article <k7qt7u$t34$1...@newscl01ah.mathworks.com>,
> "Ha " <scif...@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 :-)
0 new messages