Delimited Pattern Matching

DESCRIPTION:

Match delimited substrings in a character vector, with proper nesting.

USAGE:

delimMatch(x, delims=c("{", "}"), syntax="Rd", escapeChar="\\", lineCommentChar="%")

REQUIRED ARGUMENTS:

x
a character vector.

OPTIONAL ARGUMENTS:

delims
a character vector of length two giving the start and end delimiters.
syntax
a character string specifying what syntax to use. Currently, it is always "Rd" indicating the R help file syntax. NOTE: This argument is ignored in S-PLUS. In the future it could know about other syntaxes, allowing automatic setting of the delimiter, escape and comment characters.
escapeChar
a character string giving the character used to turn off special meaning of delimiter and comment characters. The default is backslash, "\\".
lineCommentChar
a character string specifying the characters that start a comment, causing all characters following the comment character(s) up to the end of the line to be ignored. The default is "%".

VALUE:

An integer vector of the same length as x giving the starting position (in characters) of the first match, or -1 if there is none, with attribute "match.length" giving the length (in characters) of the matched text (or -1 for no match).

DETAILS:

This function correctly handles nested delimiters.

The return value of this function has the same format as the value of the function regexpr .

This function was added for compatibility with R. In R, the function delimMatch is located in the "tools" package. The S-PLUS version of this function differs from the R version in several ways:
- the delimiter characters can be more than one character long. For example, delim=c("/*", "*/") is valid in S-PLUS.
- the syntax argument is ignored in S-PLUS.
- the S-PLUS version of the function has these additional arguments: escapeChar and lineCommentChar. Their default values are the same as those used for the escape character and the line comment character in R.

SEE ALSO:

, , , .

EXAMPLES:

x <- c("value{foo}", "function(bar)", "% ignore these {foo} and (bar)",
"foo1(foo2(x) + foo3(y))")
r <-delimMatch(x)
substring(x, r, r + attr(r, "match.length") - 1)
# returns: "{foo}"  ""  ""  ""
r <-delimMatch(x,  c("(", ")"))
substring(x, r, r + attr(r, "match.length") - 1)
# returns: ""  "(bar)"  ""  "(foo2(x) + foo3(y))"

# Search for C style comments, ignoring C++ style comment lines
x <- readLines(file.path(getenv("SHOME"), "include", "S.h"))
r <- delimMatch(x, delim = c("/*", "*/"), lineCommentChar = "//")
y <- substring(x, r, r + attr(r, "match.length") - 1)
y[nchar(y) > 0]