Thread Subject: REGEXP extract data

Subject: REGEXP extract data

From: Pete sherer

Date: 18 Jan, 2008 19:00:25

Message: 1 of 3

I have character data like:

aa = {'BI234','EVB67','C4566','St Vermont','DD'};
How can I use REGEXP to separate the character and the number?

so
aa1 = {'BI','EVB','C','St Vermont','DD'};
aa2 = {'234','67','4566','',''};;

Thanks for your suggestions,

Subject: REGEXP extract data

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 18 Jan, 2008 19:26:00

Message: 2 of 3

In article <fmqt09$mtb$1@fred.mathworks.com>, Pete sherer <tsh@abg.com> wrote:
>I have character data like:

>aa = {'BI234','EVB67','C4566','St Vermont','DD'};
>How can I use REGEXP to separate the character and the number?

>so
>aa1 = {'BI','EVB','C','St Vermont','DD'};
>aa2 = {'234','67','4566','',''};;

Well, you could certainly do it with regexp, but if those
strings illustrate the complete range of variations, then why not
use find(ismember(aa{i}, '0':'9'), 1) to find the index
of the first digit in aa{i} ? If you need to run over the entire
aa cell, you can either loop or cellfun() with UniformOutput 0 .
Note: the output of the find() will be empty for strings without digits.
--
   "Any sufficiently advanced bug is indistinguishable from a feature."
   -- Rich Kulawiec

Subject: REGEXP extract data

From: Jason Breslau

Date: 18 Jan, 2008 20:10:18

Message: 3 of 3

Assuming your data is all non-digits, followed by digits, try using
named tokens:

 >> names = regexp(aa, '^(?<aa1>\D*+)(?<aa2>\d*+)$', 'names');
 >> names = [names{:}];
 >> aa1 = {names.aa1}

aa1 =

     'BI' 'EVB' 'C' 'St Vermont' 'DD'

 >> aa2 = {names.aa2}

aa2 =

     '234' '67' '4566' '' ''

-=>J

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com