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:
Regexp Challenge

Subject: Regexp Challenge

From: Will Campbell

Date: 19 Jan, 2010 23:08:52

Message: 1 of 8

Hello,
I would like all the text in a cellstr that follows the last single slash in
each string. I would like to find a particular character in this text and
replace it with another.

Example:
    Given A = {'example/Controller/Gain 3'; 'example/Controller/Input
(deg//s)'};

    Then I want B = {'Gain 3'; 'Input (deg//s)'};

    As I work my way towards replacing all the spaces with underscores:
    C = {'Gain_3'; 'Input_(deg//s)'};

I can get do this with one line if it's a char:
    strrep(A{2}(max(regexp(A{2},'[^/]/[^/]'))+2:end),' ','_')

Without resorting to a for loop, how do I make this work with a cellstr? Is
there a better, purely regexp way of doing this?

Thanks!
-Will

Subject: Regexp Challenge

From: Nathan

Date: 19 Jan, 2010 23:17:38

Message: 2 of 8

On Jan 19, 3:08 pm, "Will Campbell" <will.campb...@mathworks.com>
wrote:
> Hello,
> I would like all the text in a cellstr that follows the last single slash in
> each string.  I would like to find a particular character in this text and
> replace it with another.
>
> Example:
>     Given A = {'example/Controller/Gain 3'; 'example/Controller/Input
> (deg//s)'};
>
>     Then I want B = {'Gain 3'; 'Input (deg//s)'};
>
>     As I work my way towards replacing all the spaces with underscores:
>     C = {'Gain_3'; 'Input_(deg//s)'};
>
> I can get do this with one line if it's a char:
>     strrep(A{2}(max(regexp(A{2},'[^/]/[^/]'))+2:end),' ','_')
>
> Without resorting to a for loop, how do I make this work with a cellstr?  Is
> there a better, purely regexp way of doing this?
>
> Thanks!
> -Will

cellfun(@(x)strrep(x(max(regexp(x,'[^/]/[^/]'))+2:end),'
','_'),A,'uni',false)
ans =
    'Gain_3'
    'Input_(deg//s)'

Does that do it for you?

-Nathan

Subject: Regexp Challenge

From: Matt Fig

Date: 20 Jan, 2010 03:02:08

Message: 3 of 8

Another approach:

regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')

Subject: Regexp Challenge

From: Jan Simon

Date: 20 Jan, 2010 12:51:05

Message: 4 of 8

Dear Matt!

> regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')

Your REGEXPREP is fine and fast (I think - I do not dare to copy it).
But it is not nice Matlab poetry.
It looks like someone has rolled an angry armadillo over the keyboard.

Not your fault! Jan

Subject: Regexp Challenge

From: Will Campbell

Date: 20 Jan, 2010 16:12:51

Message: 5 of 8

>> regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
>
> Your REGEXPREP is fine and fast (I think - I do not dare to copy it).
> But it is not nice Matlab poetry.
> It looks like someone has rolled an angry armadillo over the keyboard.
>
> Not your fault! Jan

I stared at this command for 20 minutes before I finally felt comfortable
that I understood what it was doing. You're finding character arrays that
end with a single / and removing them from the returned value, yes? I agree
with Jan that it doesn't look pretty to those not versed in regexp. But I'd
hardly call it lacking in poetry. It's an acquired taste, like
improvisation jazz.

Thanks for the help!
-Will

Subject: Regexp Challenge

From: Jason Breslau

Date: 20 Jan, 2010 16:26:49

Message: 6 of 8

Try this:

regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})

Subject: Regexp Challenge

From: Matt Fig

Date: 20 Jan, 2010 16:31:03

Message: 7 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hj6u7p$8cr$1@fred.mathworks.com>...
> Dear Matt!
>
> > regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
>
> Your REGEXPREP is fine and fast (I think - I do not dare to copy it).
> But it is not nice Matlab poetry.
> It looks like someone has rolled an angry armadillo over the keyboard.
>
> Not your fault! Jan


Angry Armadillo? Now that is funny! I guess it is better than a drunken monkey ;-)

Subject: Regexp Challenge

From: Jan Simon

Date: 20 Jan, 2010 22:25:05

Message: 8 of 8

Dear Matt!

> Angry Armadillo? Now that is funny! I guess it is better than a drunken monkey ;-)

It depends on whether you are hungry.

A short trio of Bruno, Jason and Matt:
  AB = A+B; D = AB(bsxfun(@plus,C(:,k),(0:ny-1)*Nx));
  regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
  regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})
  regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
  regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})
  AB = A+B; D = AB(bsxfun(@plus,C(:,k),(0:ny-1)*Nx));

Sing it loud and clear and please somebody to drone the base drums "cheap cheap Nike shoes, cheap cheap Nike shoes, ..."

Thanks, Jan

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