NAME
Text::Balanced - Extract delimited text sequences from strings.
SYNOPSIS
use Text::Balanced qw (
extract_delimited
extract_bracketed
extract_quotelike
extract_codeblock
);
# Extract the initial substring of $text which is delimited by
# two (unescaped) instances of the first character in $delim.
($extracted, $remainder) = extract_delimited($text,$delim);
# Extract the initial substring of $text which is bracketed
# with a delimiter(s) specified by $delim (where the string
# in $delim contains one or more of '(){}[]<>').
($extracted, $remainder) = extract_bracketed($text,$delim);
# Extract the initial substring of $text which represents a
# Perl "quote or quote-like operation"
($extracted, $remainder) = extract_quotelike($text);
# Extract the initial substring of $text which represents a block
# of Perl code, bracketed by any of character(s) specified by $delim
# (where the string $delim contains one or more of '(){}[]<>').
($extracted, $remainder) = extract_codeblock($text,$delim);
DESCRIPTION
extract_delimited
The extract_delimited
function formalizes the common idiom of extracting a single-character-delimited substring from the start of a string. For example, to extract a single-quote delimited string, the following code is typically used:
($remainder = $text) =~ s/\A('(\\'|[^'])*')//s;
$extracted = $1;
extract_delimited
takes three scalars (the text, the delimiters, and a prefix to be skipped) and extracts the initial substring of the text which is appropriately delimited. If the delimiter string has multiple characters, the first one encountered in the text is taken to delimit the substring (the delimiters are simply interpolated into a regex character class, so care is needed with some pattern metacharacters). The third argument specifies a prefix pattern which is to be skipped (but must be present!) before the substring is extracted.
All arguments are optional. If the prefix is not specified, the pattern '\s*'
- optional whitespace - is used. If the delimiter set is also not specified, the set /["'`]/
is used. If the text to be processed is not specified either, $_
is used.
extract_delimited
returns a array of three elements, the extracted substring (including the surrounding delimiters), the remainder of the text, and the skipped prefix (if any).
If a suitable delimited substring is not found, the first element of the array is the empty string, the second is the complete original text, and the prefix returned in the third element is an empty string.
Examples:
# Remove a single-quoted substring from the very beginning of $text:
($substring,$text) = extract_delimited($text, "'", '');
# Extract a single- or double- quoted substring from the
# beginning of $text, optionally after some whitespace:
($substring) = extract_delimited $text, q{"'};
# Delete the substring delimited by the first '/' in $text:
$text = join '', (extract_delimited($text,'/','[^/]*')[2,1];
Note that this last example is not the same as deleting the first quote-like pattern. For instance, if $text
contained the string:
"if ('./cmd' =~ m/$UNIXCMD/s) { $cmd = $1; }"
then after the deletion it would contain:
"if ('.$UNIXCMD/s) { $cmd = $1; }"
not:
"if ('./cmd' =~ ms) { $cmd = $1; }"
See "extract_quotelike" for a (partial) solution to this problem.
extract_bracketed
Like "extract_delimited"
, the extract_bracketed
function takes up to three optional scalar arguments: a string to extract from, a delimiter specifier, and a prefix pattern. As before, a missing prefix defaults to optional whitespace and a missing text defaults to $_
. However, a missing delimiter specifier defaults to '{}()[]<>'
(see below).
Also like extract_delimited
, extract_bracketed
returns an array consisting of the matched substring, the remainder of the text, and the skipped prefix (if any).
extract_bracketed
extracts a balanced-bracket-delimited substring (using any one (or more) of the user-specified delimiter brackets: '(..)', '{..}', '[..]', or '<..>').
A "delimiter bracket" is a bracket in list of delimiters passed as extract_bracketed
's second argument. Delimiter brackets are specified by giving either the left or right (or both!) versions of the required bracket(s). Note that the order in which two or more delimiter brackets are specified is not significant.
A "balanced-bracket-delimited substring" is a substring bounded by matched brackets, such that any other (left or right) delimiter bracket within the substring is also matched by an opposite (right or left) delimiter bracket at the same level of nesting. Any type of bracket not in the delimiter list is treated as an ordinary character.
In other words, each type of bracket specified as a delimiter must be balanced and correctly nested within the substring, and any other kind of ("non-delimiter") bracket in the substring is ignored.
For example, given the string:
$text = "{ an '[irregularly :-(] {} parenthesized >:-)' string }";
then the call:
extract_bracketed( $text, '{}' );
would return:
( "{ an '[irregularly :-(] {} parenthesized >:-)' string }" , "" , "" )
since both sets of '{..}'
brackets are properly nested and evenly balanced.
Likewise the call:
extract_bracketed( $text, '{[' );
would return the same result, since all sets of both types of specified delimiter brackets are correctly nested and balanced.
However, the call:
extract_bracketed( $text, '{([<' );
would fail, returning:
( "" , "{ an '[irregularly :-(] {} parenthesized >:-)' string }" , "" );
because the embedded pairs of '(..)'
s and '[..]'
s are "cross-nested" and the embedded '>'
is unbalanced.
Note that the embedded single-quotes in the string don't help in this case, since they treated as non-delimiter characters and therefore ignored. See "extract_quotelike" and "extract_codeblock", which do understand the nesting behaviour of the various species of Perl quotes.
extract_quotelike
extract_quotelike
attempts to recognize, extract, and segment any one of the various Perl quotes and quotelike operators (see perlop(3)) Nested backslashed delimiters, embedded balanced bracket delimiters (for the quotelike operators), and trailing modifiers are all caught. For example:
'q # a literal octothorpe: \# (not the end of the q!) #'
' "You said, \"Use sed\"." '
' s{([A-Z]{1,8}\.[A-Z]{3})} /\L$1\E/; '
' tr/\\\/\\\\/\\\//ds; '
are all extracted correctly.
Note however that extract_quotelike
does not recognize the vaguely deprecated ?....?
pattern match operator. This is not because it is frowned upon, but simply because it is not obvious how to distinguish from the ternary operator (?:
), without deciphering the entire Perl semantics of the processed text [suggestions are welcome].
Note too that, when using the /x modifier on a regex, any comment containing the current pattern delimiter will cause the regex to be immediately terminated. In other words:
'm /
(?i) # CASE INSENSITIVE
[a-z_] # LEADING ALPHABETIC/UNDERSCORE
[a-z0-9]* # FOLLOWED BY ANY NUMBER OF ALPHANUMERICS
/x'
will be extracted as if it were:
'm /
(?i) # CASE INSENSITIVE
[a-z_] # LEADING ALPHABETIC/'
This behaviour is identical to that of the Perl 5.004 interpreter.
extract_quotelike
takes two arguments: the text to be processed and a prefix to be matched at the very beginning of the text. If no prefix is specified, optional whitespace is the default. If no text is given, $_
is used.
An array of 11 elements is returned. The elements are:
- [0]
-
the extracted quotelike substring (including trailing modifiers),
- [1]
-
the remainder of the input text,
- [2]
-
the prefix substring (if any),
- [3]
-
the name of the quotelike operator (if any),
- [4]
-
the left delimiter of the first block of the operation,
- [5]
-
the text of the first block of the operation (that is, the contents of a quote, the regex of a match or substitution or the target list of a translation),
- [6]
-
the right delimiter of the first block of the operation,
- [7]
-
the left delimiter of the second block of the operation (that is, if it is a
s
,tr
, ory
), - [8]
-
the text of the second block of the operation (that is, the replacement of a substitution or the translation list of a translation),
- [9]
-
the right delimiter of the second block of the operation (if any),
- [10]
-
the trailing modifiers on the operation (if any).
For each of the fields marked "(if any)" the default value is an empty string.
Examples:
# Extract the first quotelike literal that appears in text
($quotelike) = extract_quotelike($text,'.*?');
# Replace one or more leading whitespace-separated quotelike
# literals in $_ with "<QLL>"
do { $_ = join '<QLL>', (extract_quotelike)[2,1] } until $@;
# Isolate the search pattern in a quotelike operation from $text
($op,$pat) = (extract_quotelike $text)[3,5];
if ($op =~ /[ms]/)
{
print "search pattern: $pat\n";
}
else
{
print "$op is not a pattern matching operation\n";
}
extract_codeblock
extract_codeblock
attempts to recognize and extract a balanced bracket delimited substring which may contain unbalanced brackets inside Perl quotes or quotelike operations. That is, extract_codeblock
is like a combination of "extract_bracketed"
and "extract_quotelike"
.
extract_codeblock
takes the same three parameters as extract_bracketed
: a text to process, a set of delimiter brackets to look for, and a prefix to match first. No prefix argument implies optional whitespace at the start, no delimiter brackets indciates that only '{'
is to be used (unlike extract_delimited
), no text means process $_
.
Once the prefix has been disposed of, code blocks are extracted by stepping through the text and trying the following alternatives in sequence:
Try and match a closing delimiter bracket. If the bracket was the same species as the last opening bracket, return the substring to that point. If the bracket was mismatched, return an error.
Try to match a quote or quotelike operator. If found, call
extract_quotelike
to eat it. Ifextract_quotelike
fails, return the error it returned. Otherwise go back to step 1.Try to match an opening delimiter bracket. If found, call
extract_codeblock
recursively to eat the embedded block. If the recursive call fails, return an error. Otherwise, go back to step 1.Unconditionally match a bareword or any other single character, and then go back to step 1.
Examples:
# Find a while loop in the text
if ($text =~ s/.*?while\s*\{/{/)
{
$loop = "while " . (extract_codeblock $text)[0];
}
# Remove the first round-bracketed list (which may include
# round- or curly-bracketed code blocks or quotelike operators)
$text = join '', (extract_codeblock($text,"(){}",'[^(]*')[2,1];
DIAGNOSTICS
All the functions return ('',$original_text,'')
on failure. In addition, one of the following explanatory diagnostic messages is returned in the $@
variable:
Did not find a suitable bracket: "%s"
-
The delimiter provided to
extract_bracketed
was not one of'()[]<>{}'
. Did not find prefix: /%s/
-
A non-optional prefix was specified but wasn't found at the start of the text.
Could not extract "%c"-delimited substring
-
extract_delimited
,extract_quotelike
orextract_codeblock
couldn't find an initial substring (after the prefix) which was delimited by the delimiter(s) specified. Did not find opening bracket after prefix: "%s"
-
extract_bracketed
orextract_codeblock
was expecting a particular kind of bracket at the start of the text, and didn't find it. No quotelike operator found after prefix: "%s"
-
extract_quotelike
didn't find one of the quotelike operatorsq
,qq
,qw
,qx
,s
,tr
ory
at the start of the substring it was extracting. Unmatched closing bracket: "%c"
-
extract_bracketed
,extract_quotelike
orextract_codeblock
encountered a closing bracket where none was expected. Unmatched opening bracket(s): "%s"
-
extract_bracketed
,extract_quotelike
orextract_codeblock
ran out of characters in the text before closing one or more levels of nested brackets. Mismatched closing bracket: expected "%c" but found "%s"
-
extract_bracketed
,extract_quotelike
orextract_codeblock
found a valid bracket delimiter, but it was the wrong species. This usually indicates a nesting error, but may indicate incorrect quoting or escaping. No block delimiter found after quotelike "%s"
-
extract_quotelike
orextract_codeblock
found one of the quotelike operatorsq
,qq
,qw
,qx
,s
,tr
ory
without a suitable block after it. Missing second block for quotelike "%s"
-
extract_codeblock
orextract_quotelike
found one of the quotelike operatorss
,tr
ory
followed by only one block. Nested codeblock failed to balance from "%s..."
-
A block within the substring being extracted by
extract_codeblock
was incorrectly nested or had a unmatched delimiter in it.
AUTHOR
Damian Conway (damian@cs.monash.edu.au)
BUGS AND IRRITATIONS
There are undoubtedly serious bugs lurking somewhere in this code, if only because it gives the impression of understanding a great deal more about Perl than it really does. Remember in particular, that neither extract_quotelike
nor extract_codeblock
honour ( or even know about) ?...?
patterns.
Bug reports and other feedback are most welcome.
COPYRIGHT
Copyright (c) 1997, Damian Conway. All Rights Reserved.
This module is free software. It may be used, redistributed
and/or modified under the terms of the Perl Artistic License
(see http://www.perl.com/perl/misc/Artistic.html)