[R] Help with regular expressions.

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue Feb 9 04:09:08 CET 2021


Simpler, but would fail if there are more "."s beyond the second (it
changes the last one to a "-"):

> sub("(.*)\\.([^.]*)", "\\1-\\2", "aa.bcv.cdg")
[1] "aa.bcv-cdg"


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Feb 8, 2021 at 6:49 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> > gsub("(.*\\.[^.]*)\\.(.*)","\\1-\\2", "aa.bcv.cdg")
> [1] "aa.bcv-cdg"
>
> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Feb 8, 2021 at 6:29 PM Rolf Turner <r.turner using auckland.ac.nz>
> wrote:
>
>>
>> I want to deal with strings of the form "a.b.c" and to change (using
>> sub() or whatever is appropriate) the second "." to a "-", i.e. to
>> change "a.b.c" to "a.b-c".  I want to leave the first "." as-is.
>>
>> I guess I could do a gsub(), changing all "."s to "-"s, and then do
>> a sub() changing the first "-" back to a ".".  But this seems very
>> kludgy.  There must be a sexier way.  Mustn't there?  Is there regular
>> expression syntax for picking out the second occurence of a particular
>> string?
>>
>> cheers,
>>
>> Rolf Turner
>>
>> --
>> Honorary Research Fellow
>> Department of Statistics
>> University of Auckland
>> Phone: +64-9-373-7599 ext. 88276
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list