XML technologies / Regular expressions / Perl / The split operator / The regex operand with capturing parentheses
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
When the match operand contains capturing parentheses, split behaves quite differently. The returned list also contains the text captured between the ordinary pieces. Text normally removed by split is therefore returned as well.
An example of processing HTML data: split(/(<[^>]*>)/) transforms
...●und●<B>sehr●<FONT●color=red>sehr</FONT>●viel</B>●Mühe...
into
( '...●und●', '<B>', 'sehr●', '<FONT●color=red>', 'sehr', '</FONT>', '●viel', '</B>', '●Mühe...')
Without capturing parentheses, split(/<[^>]*>/) returns these elements:
( '...●und●', 'sehr●', 'sehr', '●viel', '●Mühe...' )
These extra elements do not count toward the maximum set by the limit operand. The limit concerns the number of pieces into which the input string is split, rather than the number of returned list elements.
With multiple pairs of parentheses, each pair produces an extra element. If a pair does not participate in the match and captures no text, an element with an undefined value is still produced.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top