필터 지우기
필터 지우기

how to extract strings between two newline characters using regexp

조회 수: 32 (최근 30일)
Li Xue
Li Xue 2017년 7월 12일
댓글: Li Xue 2017년 7월 13일
I have a string:
S = sprintf('\n1 2\n3 4\n')
And I want to extract '1 2' into a cell and '3 4' into a cell, using the following code:
a=regexp(S, '\n\d+[^\n]+\n','match')
But '3 4' was not extracted.
Did I do something wrong?

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 12일
When you used
a=regexp(S, '\n\d+[^\n]+\n','match')
then the \n at the end "eats" the \n after '1 2', leaving the remaining string as '3 4\n' . That string then does not match the pattern that begins with \n
Consider
a = regexp(S, '(?<=\n)\d[^\n]*', 'match')
This leaves the \n in place; and the (?<=\n) part requires that the \n before the digit be there but does not include the \n in the output.
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 7월 13일
overlapping in regular expressions typically requires using zero-width assertions.
Some programming languages such as a newer python regexp module support an "overlapped" switch.
In some programming language such as perl, there are tricks that can be done with essentially evaluating code in the middle of a match, but that code has to get a bit complicated to handle backtracking properly. See for example http://www.perlmonks.org/?node_id=463461
Li Xue
Li Xue 2017년 7월 13일
I see. Many thanks for your help.

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

추가 답변 (2개)

Sayam Ganguly
Sayam Ganguly 2017년 7월 12일
From your question I understand that you have a string '\n1 2\n3 4\n' and you want to extract '1 2' and '3 4' into a 1*2 cell array. I would like to suggest a different regexp that should help you achieve your workflow.
a=regexp(S, '.*','match','dotexceptnewline')
Here '.*' automatically considers all the characters but because of the 'dotexceptnewline', the '/n' characters are not considered and you get a 1*2 cell array split with your desired result. In case of your approach the entire pattern was getting matched only once and was not getting repeated.
  댓글 수: 4
Li Xue
Li Xue 2017년 7월 13일
편집: Li Xue 2017년 7월 13일
Yes, perl can achieve quite complicated tasks. But Perl regular expression rules are very simple. For a programmer, it is very easy to remember and use. But matlab regular expression has lot of tricks, such as 'dotexceptnewline'. It looks simpler than perl but on the programming part it takes more time to figure out.
Walter Roberson
Walter Roberson 2017년 7월 13일
I programmed in perl for a few years. The rules are not easy to remember. There are multiple books explaining perl regular expressions. For example O'Reilly's "Mastering Regular Expressions" http://shop.oreilly.com/product/9780596528126.do which is over 500 pages.
Most perl regular expression authors make mistakes even on comparatively simple tasks such as matching the valid floating point numbers. Hardly anyone gets right tasks such as balancing brackets (a task which is not possible with true regular expressions, and not possible with perl basic regular expressions, requiring perl extended regular expressions.)

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


Jan
Jan 2017년 7월 12일
S = sprintf('\n1 2\n3 4\n')
C = strsplit(S, '\n');
  댓글 수: 2
Li Xue
Li Xue 2017년 7월 13일
strsplit will create two additional empty cells.
Jan
Jan 2017년 7월 13일
Then add:
C(cellfun('isempty', C)) = [];

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by