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 Integer & Operator from Charater

Subject: Extract Integer & Operator from Charater

From: Nehal

Date: 30 Nov, 2010 20:40:05

Message: 1 of 11

I have a character like this:
a = '123+456=';

now I want to extract the integer and the operators.
like I want
op1 = 123
op2 = 456
and
op = +

Subject: Extract Integer & Operator from Charater

From: Walter Roberson

Date: 30 Nov, 2010 20:49:10

Message: 2 of 11

On 10-11-30 02:40 PM, Nehal wrote:
> I have a character like this: a = '123+456=';
>
> now I want to extract the integer and the operators.
> like I want op1 = 123
> op2 = 456
> and
> op = +

See regexp()

The pattern would look something like

(\d+)\s*(.)\s*(\d+)\s*(.)

but would have to be modified if the operator could potentially be more than
one character.

Subject: Extract Integer & Operator from Charater

From: Nehal

Date: 30 Nov, 2010 20:53:19

Message: 3 of 11

"Nehal " <arnab620@yahoo.com> wrote in message <id3nf5$37u$1@fred.mathworks.com>...
> I have a character like this:
> a = '123+456=';
>
> now I want to extract the integer and the operators.
> like I want
> op1 = 123
> op2 = 456
> and
> op = +

the topic name should be "character" not Charater... typing mistake

Subject: Extract Integer & Operator from Charater

From: Nehal

Date: 30 Nov, 2010 21:26:06

Message: 4 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <id3o08$7f1$1@canopus.cc.umanitoba.ca>...
> See regexp()
>
> The pattern would look something like
>
> (\d+)\s*(.)\s*(\d+)\s*(.)
>
> but would have to be modified if the operator could potentially be more than
> one character.

is there any other method...?

Subject: Extract Integer & Operator from Charater

From: Walter Roberson

Date: 30 Nov, 2010 22:22:00

Message: 5 of 11

On 10-11-30 03:26 PM, Nehal wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <id3o08$7f1$1@canopus.cc.umanitoba.ca>...
>> See regexp()
>>
>> The pattern would look something like
>>
>> (\d+)\s*(.)\s*(\d+)\s*(.)
>>
>> but would have to be modified if the operator could potentially be
>> more than one character.
>
> is there any other method...?

There are many other methods. For example, ismember(a,'+=') would return a
logical vector indicating whether each position in a was '+' or '='. The
positions that *are* are your operators. find() on that returns the indices.
Position 1 through one before the first operator is your first number,
possibly with spaces or non-digit characters in it.

And so on. You can make it complex or you can make it pretty simple to code.
regexp() is pretty simple to code *and* has the advantage of making it
relatively easy to specify exactly which characters you want to be valid.

Subject: Extract Integer & Operator from Charater

From: Matt Fig

Date: 30 Nov, 2010 23:11:05

Message: 6 of 11

Something like this would work:

T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match')

Then even if one of the terms has a decimal, you are covered. For example:

a = '1.23+45.666=';
T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match');
T{:}
ans =
    '1.23' '45.666'
ans =
    '+'

So T{1} is the LHS, T{2} is the RHS, T{3} is the operation.

Subject: Extract Integer & Operator from Charater

From: Nehal

Date: 1 Dec, 2010 14:44:07

Message: 7 of 11

"Matt Fig" <spamanon@yahoo.com> wrote in message <id40a9$cd5$1@fred.mathworks.com>...
> Something like this would work:
>
> T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match')
>
> Then even if one of the terms has a decimal, you are covered. For example:
>
> a = '1.23+45.666=';
> T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match');
> T{:}
> ans =
> '1.23' '45.666'
> ans =
> '+'
>
> So T{1} is the LHS, T{2} is the RHS, T{3} is the operation.

I have tried this one. It worked only when there is '+'. What if I have '-'. Because in my project, I'll not know what is given as input. But it will either '+' or '-'. So I have to take care of both the conditions.

Subject: Extract Integer & Operator from Charater

From: Walter Roberson

Date: 1 Dec, 2010 18:55:48

Message: 8 of 11

On 01/12/10 8:44 AM, Nehal wrote:
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> <id40a9$cd5$1@fred.mathworks.com>...
>> Something like this would work:
>>
>> T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match')

Careful, [+-*/] means "the set of characters including '/' and all
characters in the range '+' to '*'.

To get a literal '-' it needs to be either the last character in the set
or else it needs to be the first character (after a possible '^'):

[-+*/]
[+*/-]

Subject: Extract Integer & Operator from Charater

From: Matt Fig

Date: 1 Dec, 2010 20:11:05

Message: 9 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <VSwJo.660$hy4.66@newsfe17.iad>...
> On 01/12/10 8:44 AM, Nehal wrote:
> > "Matt Fig" <spamanon@yahoo.com> wrote in message
> > <id40a9$cd5$1@fred.mathworks.com>...
> >> Something like this would work:
> >>
> >> T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match')
>
> Careful, [+-*/] means "the set of characters including '/' and all
> characters in the range '+' to '*'.
>
> To get a literal '-' it needs to be either the last character in the set
> or else it needs to be the first character (after a possible '^'):
>
> [-+*/]
> [+*/-]

Or I needed to use the proper escape character. (Oops)

regexp(a,{'\d+\.*\d+','[+\-*/]'},'match');

Subject: Extract Integer & Operator from Charater

From: Walter Roberson

Date: 1 Dec, 2010 21:11:18

Message: 10 of 11

On 10-12-01 02:11 PM, Matt Fig wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <VSwJo.660$hy4.66@newsfe17.iad>...
>> On 01/12/10 8:44 AM, Nehal wrote:
>> > "Matt Fig" <spamanon@yahoo.com> wrote in message
>> > <id40a9$cd5$1@fred.mathworks.com>...
>> >> Something like this would work:
>> >>
>> >> T = regexp(a,{'\d+\.*\d+','[+-*/]'},'match')
>>
>> Careful, [+-*/] means "the set of characters including '/' and all
>> characters in the range '+' to '*'.
>>
>> To get a literal '-' it needs to be either the last character in the
>> set or else it needs to be the first character (after a possible '^'):
>>
>> [-+*/]
>> [+*/-]
>
> Or I needed to use the proper escape character. (Oops)
>
> regexp(a,{'\d+\.*\d+','[+\-*/]'},'match');

The "Character Classes" description of the Regular Expressions section does
not document using the escape character as a means to remove the special
meaning of '-' within []

Mind you, it also doesn't document it as a possibility for removing the
special meaning of ^ as in [\^]

Subject: Extract Integer & Operator from Charater

From: Matt Fig

Date: 1 Dec, 2010 21:34:05

Message: 11 of 11

Walter Roberson <roberson@hushmail.com> wrote in message
> The "Character Classes" description of the Regular Expressions section does
> not document using the escape character as a means to remove the special
> meaning of '-' within []
>
> Mind you, it also doesn't document it as a possibility for removing the
> special meaning of ^ as in [\^]


It is not specifically mentioned in the "Character Classes" section, but in the "Character Representation" section it says:

"If a character has special meaning in a regular expression, precede
it with backslash (\) to match it literally."

Which seems to me general enough to cover the cases you mention.

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