Thread Subject: simple regexp question

Subject: simple regexp question

From: matt dash

Date: 12 Aug, 2008 16:13:01

Message: 1 of 6

Hello,

I have a cell array of strings, each of which looks like :

'Lambda46' => '54.954684760469313',

(including the quotes and comma) and I want to extract the
two numbers 46 and 54.954... and put them in vectors.
Normally I would do this by finding the locations of the
quotes and working from there, but I suspect there's a
better way using regular expressions and I figure it's about
time I started using them... is there a simple way to do this?

Subject: simple regexp question

From: Clemens Winkler

Date: 12 Aug, 2008 16:20:59

Message: 2 of 6

On 12 Aug., 18:13, "matt dash" <n...@mail.com> wrote:
> Hello,
>
> I have a cell array of strings, each of which looks like :
>
> 'Lambda46' => '54.954684760469313',

You should give more informations. Do they all look like
'Lambdaxx' => 'xxx',
??

Or are there any other characters as well?? Is it always Lambda?

Clemens

Subject: simple regexp question

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

Date: 12 Aug, 2008 16:30:22

Message: 3 of 6

In article <g7scqd$lbp$1@fred.mathworks.com>, matt dash <n.a@mail.com> wrote:

>I have a cell array of strings, each of which looks like :

>'Lambda46' => '54.954684760469313',

>(including the quotes and comma) and I want to extract the
>two numbers 46 and 54.954... and put them in vectors.
>Normally I would do this by finding the locations of the
>quotes and working from there, but I suspect there's a
>better way using regular expressions and I figure it's about
>time I started using them... is there a simple way to do this?

You don't quite give enough information about the form of the
numbers, and you don't give us any information about whether
there can be digits in the identifier before the trailing digits
that you want to extract (e.g., 'Rom3Gam17')

(\d+).*((?:\d+(?:\.\d*))|(?:\.\d+))

The above expression assumes that the two set of digits
on the line are to be extracted. It allows three forms
for the second number: an integer with no decimal place;
or a fixed-point number with digits followed by decimal point
optionally followed by digits; or a fixed-point number that starts
immediately with a decimal point followed by {mandatory} digits.
It does not allow the second "number" to consistent entirely of
a decimal point with no digits. The complexity of the expression
could potentially be much reduced if it was known that some
of these forms never occur.
--
  "What is important, then, is not that the critic should possess a
  correct abstract definition of beauty for the intellect, but a
  certain kind of temperament, the power of being deeply moved by
  the presence of beautiful objects." -- Walter Pater

Subject: simple regexp question

From: matt dash

Date: 12 Aug, 2008 16:34:03

Message: 4 of 6

Clemens Winkler <ClemensWinkler@gmx.net> wrote in message
<a735e4fc-1249-4065-92cd-132d2f9b7fb9@m73g2000hsh.googlegroups.com>...
> On 12 Aug., 18:13, "matt dash" <n...@mail.com> wrote:
> > Hello,
> >
> > I have a cell array of strings, each of which looks like :
> >
> > 'Lambda46' => '54.954684760469313',
>
> You should give more informations. Do they all look like
> 'Lambdaxx' => 'xxx',
> ??
>
> Or are there any other characters as well?? Is it always
Lambda?
>
> Clemens


yes the only thing that changes is the numbers, which are
random and of arbitrary length.

Subject: simple regexp question

From: Doug Schwarz

Date: 12 Aug, 2008 17:33:47

Message: 5 of 6

In article <g7scqd$lbp$1@fred.mathworks.com>,
 "matt dash" <n.a@mail.com> wrote:

> Hello,
>
> I have a cell array of strings, each of which looks like :
>
> 'Lambda46' => '54.954684760469313',
>
> (including the quotes and comma) and I want to extract the
> two numbers 46 and 54.954... and put them in vectors.
> Normally I would do this by finding the locations of the
> quotes and working from there, but I suspect there's a
> better way using regular expressions and I figure it's about
> time I started using them... is there a simple way to do this?

I know you asked for a solution using regular expressions, but really
this sort of problem is ideal for textscan. Suppose your cell array of
strings is C, then

  str = sprintf('%s\n',C{:});
  a = textscan(str,'''Lambda%n'' => ''%n'',');
  col1 = a{1};
  col2 = a{2};

will give you all the first numbers in a vector, col1, and all the
second numbers in a vector, col2.

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

Subject: simple regexp question

From: matt dash

Date: 12 Aug, 2008 17:45:04

Message: 6 of 6

Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-B4663B.13334712082008@softbank060082049208.bbtec.net>...
> In article <g7scqd$lbp$1@fred.mathworks.com>,
> "matt dash" <n.a@mail.com> wrote:
>
> > Hello,
> >
> > I have a cell array of strings, each of which looks like :
> >
> > 'Lambda46' => '54.954684760469313',
> >
> > (including the quotes and comma) and I want to extract the
> > two numbers 46 and 54.954... and put them in vectors.
> > Normally I would do this by finding the locations of the
> > quotes and working from there, but I suspect there's a
> > better way using regular expressions and I figure it's about
> > time I started using them... is there a simple way to do
this?
>
> I know you asked for a solution using regular expressions,
but really
> this sort of problem is ideal for textscan. Suppose your
cell array of
> strings is C, then
>
> str = sprintf('%s\n',C{:});
> a = textscan(str,'''Lambda%n'' => ''%n'',');
> col1 = a{1};
> col2 = a{2};
>
> will give you all the first numbers in a vector, col1, and
all the
> second numbers in a vector, col2.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Thanks, thats just what I was looking for.

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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com