[R] RegExp question
David Winsemius
dwinsemius at comcast.net
Wed Jun 16 18:47:28 CEST 2010
On Jun 16, 2010, at 12:04 PM, Andrej wrote:
> Dear all,
>
> I'm trying to filter out the "number of leaves" (it should be 1 in the
> example below) from the following string:
>
>> string
> [1] "Java-Object{J48 pruned tree\n------------------\n: 0 (15.0/3.0)\n
> \nNumber of Leaves : \t1\n\nSize of the tree : \t1\n}"
>
> Any idea how to do that as simple as possible? Thanks in advance for
> any advice.
?sub # or ?gsub if you need more than one pattern matched (they are
on the same page).
This should find the first occurrence of digits following a tab
terminated by a line feed and then return only the digits:
string <- "Java-Object{J48 pruned tree\n------------------\n: 0
(15.0/3.0)\n \nNumber of Leaves : \t1\n\nSize of the tree : \t1\n}"
sub("^.+\\t(\\d+)\\n.+$", "\\1", string)
[1] "1"
The parens within the search pattern are matched to "\\1". Need to
double backslashed within patterns.
>
> Regards, Andrej
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list