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

New to MATLAB?

How do i use regular expressions effectively for parsing my text?

Asked by Phanichin

Phanichin (view profile)

on 19 Jun 2013

Hello,

I am currently using regular expressions for parsing my text. The pattern is:

pattern = 'red(dy|dish)?'
regexp(text,pattern,'match')

the answer is reddy, reddish, red. However the pattern recognizes red in words like predominantly , predetermined etc. I do not want this to happen. I want the regular expressions to only return words which describe color red (example red, reddy, reddish). How do i change my pattern or regular expression accordingly? Any help in this regard would be highly appreciated.

Thanks, Phani

0 Comments

Phanichin

Phanichin (view profile)

Products

4 Answers

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 19 Jun 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 24 Jun 2013
Accepted answer

try:

pattern = '\<red(dy|dish)?\s'

ADD

pattern = '\<red(d)?(y|ish)?\>'

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 19 Jun 2013
Edited by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 19 Jun 2013
text='red   redy predominant red ade redish   red  vrd redishh'
pattern='(?<=\s)red(y|ish)?(?!\S)'
regexp([' ' text],pattern,'match')

0 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Phanichin

Phanichin (view profile)

on 20 Jun 2013

Thank you Andrei and Azzi. Both the solutions are working. I cannot distinguish between these two though.

2 Comments

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 20 Jun 2013

They are different solutions and giving different results

Phanichin

Phanichin (view profile)

on 24 Jun 2013

Hello Azzi,

Yes they are different. I tried it just now. Your solution is better. However i am having a problem. When i have text like

text = 'red, redish redy' pattern='(?<=\s)red(y|ish)?(?!\S)' regexp([' ' text],pattern,'match')

the output is only 'redish' 'redy' there is no red in the output. This is the case when the text = 'redish, red redy' . The output in this case is 'red' 'redy' there is no redish. How do i resolve this?

Regards, Phani

Phanichin

Phanichin (view profile)

Answer by Phanichin

Phanichin (view profile)

on 24 Jun 2013

Hello Azzi,

Yes they are different. I tried it just now. Your solution is better. However i am having a problem. When i have text like

text = 'red, redish redy' pattern='(?<=\s)red(y|ish)?(?!\S)' regexp([' ' text],pattern,'match')

the output is only 'redish' 'redy' there is no red in the output. This is the case when the text = 'redish, red redy' . The output in this case is 'red' 'redy' there is no redish. How do i resolve this?

Regards, Phani

1 Comment

Andrei Bobrov

Andrei Bobrov (view profile)

on 24 Jun 2013

Please see ADD part in my answer.

Phanichin

Phanichin (view profile)

Contact us