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:
extract the content within brackets

Subject: extract the content within brackets

From: Wendy

Date: 22 Oct, 2010 19:29:03

Message: 1 of 3

Hi all,

I have a cell vector, and the content of each cell looks like

'MI:0004(affinity chromatography technology)|MI:0096(pull down)'

I want to extract the content within brackets, and put them into 2 cells in another variable. I have spent a while and tried to understand the 'regexp' expressions. Following is what I got so far...


regexp(test,'\w*)\|?','match')

ans =

    'technology)|' 'down)'

Can anybody please give me some hints?

Thank you in advance,
Wendy

Subject: extract the content within brackets

From: Oleg Komarov

Date: 22 Oct, 2010 19:45:06

Message: 2 of 3

"Wendy " <wlq121@gmail.com> wrote in message <i9solv$loe$1@fred.mathworks.com>...
> Hi all,
>
> I have a cell vector, and the content of each cell looks like
>
> 'MI:0004(affinity chromatography technology)|MI:0096(pull down)'
>
> I want to extract the content within brackets, and put them into 2 cells in another variable. I have spent a while and tried to understand the 'regexp' expressions. Following is what I got so far...
>
>
> regexp(test,'\w*)\|?','match')
>
> ans =
>
> 'technology)|' 'down)'
>
> Can anybody please give me some hints?
>
> Thank you in advance,
> Wendy

If you don't tell us how do you want to part the string...
Oleg

Subject: extract the content within brackets

From: Jesse Hopkins

Date: 22 Oct, 2010 20:21:04

Message: 3 of 3

To just grab the stuff between the colons use something like this:
str = 'MI:0004(affinity chromatography technology)|MI:0096(pull down)'
x = regexp(str,':([^:]*):','tokens')

The "tokens" mode of regexp will return everything that matches within the parenthesis.

This regexp is saying "match a string that starts with a colon, then contains any number of non-colon characters, and then ends in a colon, but only return the string between the colons.

Assuming you want your cell to break on the "|" character, try this:
 x = regexp(str,':([^:\|]*)\|([^:\|]*):','tokens')

This one statement is similar to the previous, however it matches two different tokens, hence x will be a 1x2 cell array. The first element contains the text between the ":" and the "|", the second contains the text between "|" and ":". Note that I had to escape the "|" character with a "\", because otherwise the "|" has special meaning.

Tags for 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