[R] Special characters in regular expressions

Patrick Connolly p_connolly at slingshot.co.nz
Thu Sep 24 11:52:04 CEST 2015


I need to change a vector dd that looks like this:
c("LBAM 5|A|15C|3h", "LBAM 5|K|15C|2h") 

into this:
c("LBAM 5|15C|3h", "LBAM 5|15C|2h")

It's not very imaginative, but I could use a complicated nesting of
gsub() as so:

gsub("-", "\\|", gsub("K-", "", gsub("A-", "", gsub("\\|", "-", dd))))

Or I could make it a bit more readable by using interim objects, 

But I'd prefer to use a single regular expression that can detect "A|"
*and* "K|" without collateral damage from the impact of special
characters and regular characters.

TIA

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list