MATLAB Answers

Regexp question number two

조회 수: 2(최근 30일)
Lucas
Lucas 19 Sep 2012
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.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Shub
Daniel Shub 19 Sep 2012
You need to escape the `\`
results = regexp(str, '\\n', 'split')

  댓글 수: 1

Matt Tearle
Matt Tearle 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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by