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?

Regexp question number two

Asked by Lucas

Lucas (view profile)

on 19 Sep 2012
Accepted Answer by Daniel

Daniel (view profile)

I have no idea why this isn't working. I've read in several places that this should work, but it doesn't. I have a string like:

str = 'Hello\nMy name is Lucas';

and I wanted to use regexp to split at the new line character, so I have the following:

results = regexp(str, '\n', 'split');

but it doesn't split the string and just returns the original string. I just upgraded to 2012b. Thanks.



Lucas (view profile)



No products are associated with this question.

1 Answer

Answer by Daniel

Daniel (view profile)

on 19 Sep 2012
Accepted answer

You need to escape the `\`

 results = regexp(str, '\\n', 'split')

1 Comment

Matt Tearle

Matt Tearle (view profile)

on 19 Sep 2012

To (hopefully) clarify: the "\n" in your string isn't actually a newline character -- it's just the two characters \ and n. So Daniel's solution is splitting on the literal combination of characters "\n".

If you had defined your string as

str = ['Hello',10,'My name is Lucas']

then the 6th character would actually be a line break and your regexp command would have worked.


Daniel (view profile)

Contact us