[R] Extracting part of alpha numeric string

Berend Hasselman bhh at xs4all.nl
Mon Nov 30 16:34:35 CET 2015


> On 30 Nov 2015, at 14:57, phgrosjean at sciviews.org wrote:
> 
> 
>> On 30 Nov 2015, at 13:09, Abhinaba Roy <abhinabaroy09 at gmail.com> wrote:
>> 
>> Hey,
>> 
>> worked like a charm! :)
>> 
>> Could you please explain about
>> 
>> sub("^([0-9]*).*$", "\\1", fields)
>> 
> 
> Yes.
> 
> sub() replaces substrings. The first argument captures the interesting part of the string:
> 
> ^ = start of the string,
> 
> ([0-9]*) = capture of the interesting part of the string. [0-9] means any figure from 0 to 9. * means 1 or more of these characters, and () is used to capture the substring, 
> 
> .* = all the rest. Dot (.) means any character, and * means again one or more of these characters,
> 
> $ = the end of the string.

Small correction:

* means zero or more characters

according to ?regex.

Berend



More information about the R-help mailing list