Borrar filtros
Borrar filtros

how to extract strings between two newline characters using regexp

32 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de Jul. de 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 comentarios
Walter Roberson
Walter Roberson el 13 de Jul. de 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 el 13 de Jul. de 2017
I see. Many thanks for your help.

Iniciar sesión para comentar.

Más respuestas (2)

Sayam Ganguly
Sayam Ganguly el 12 de Jul. de 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 comentarios
Li Xue
Li Xue el 13 de Jul. de 2017
Editada: Li Xue el 13 de Jul. de 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 el 13 de Jul. de 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.)

Iniciar sesión para comentar.


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

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by