Effacer les filtres
Effacer les filtres

how to extract strings between two newline characters using regexp

35 vues (au cours des 30 derniers jours)
Li Xue
Li Xue le 12 Juil 2017
Commenté : Li Xue le 13 Juil 2017
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Juil 2017
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 commentaires
Walter Roberson
Walter Roberson le 13 Juil 2017
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 le 13 Juil 2017
I see. Many thanks for your help.

Connectez-vous pour commenter.

Plus de réponses (2)

Sayam Ganguly
Sayam Ganguly le 12 Juil 2017
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 commentaires
Li Xue
Li Xue le 13 Juil 2017
Modifié(e) : Li Xue le 13 Juil 2017
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 le 13 Juil 2017
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.)

Connectez-vous pour commenter.


Jan
Jan le 12 Juil 2017
S = sprintf('\n1 2\n3 4\n')
C = strsplit(S, '\n');
  2 commentaires
Li Xue
Li Xue le 13 Juil 2017
strsplit will create two additional empty cells.
Jan
Jan le 13 Juil 2017
Then add:
C(cellfun('isempty', C)) = [];

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by