Re: [R] Pattern Matching Replacement

From: Hans-Jörg Bibiko <bibiko_at_eva.mpg.de>
Date: Thu, 19 Jun 2008 23:40:32 +0200

On 19.06.2008, at 20:17, ppatel3026 wrote:

>
> I would like to replace "\r\n" with "" in a character string, where
> "\r\n"
> exists only between < and >, how could I do that?
>
> Initial:
> characterString = "<XML><tag1
> id=\"F\r\n2\"></t\r\nag1>\r\n<tag\r\n2></tag2></XML>"
>
> Result:
> characterString = "<XML><tag1 id=\"F2\"></tag1>\r\n<tag2></tag2></
> XML>"
>
> Tried with sub(below) but it only replaces the first instance and I
> am not
> sure how to pattern match so that it only replaces \r\n that exist
> within
> tags(< and >).
>
> sub("\r\n", "", charStream)

It's only a very first idea:

gsub("(?<=<)([^>]*?)\\r\\n([^>]*?)(?=>)", "\\1\\2", characterString, perl=T)

--Hans



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 19 Jun 2008 - 23:18:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 Jun 2008 - 23:31:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive