Re: [R] why does regexpr not work with '.'

From: jim holtman <jholtman_at_gmail.com>
Date: Tue, 15 Apr 2008 06:31:30 -0400

In a regular expression, '.' matches any character, which will be the first one. If you want to match a period, you have to escape it:

> f="a,b.c@d:" #define an arbitrary test string
> regexpr('.',f)
[1] 1
attr(,"match.length")
[1] 1
> regexpr('\\.',f)

[1] 4
attr(,"match.length")
[1] 1
>

On Tue, Apr 15, 2008 at 6:27 AM, Jonathan Williams <jonathan.williams_at_dpag.ox.ac.uk> wrote:
> Dear R Helpers,
>
> I am running R 2.6.2 on a Windows XP machine.
>
> I am trying to use regexpr to locate full stops in strings, but, without
> success.
>
> Here an example:-
>
> f="a,b.c_at_d:" #define an arbitrary test string
> regexpr(',',f) #find the occurrences of ',' in f - should be one at location
> 2
> # and this is what regexpr finds
> #[1] 2
> #attr(,"match.length")
> #[1] 1
>
> regexpr('@',f) #find occurrences of '@' in f - should be one at location 6
> # and this is what regexpr finds
> #[1] 6
> #attr(,"match.length")
> #[1] 1
>
> regexpr('.',f) #find the occurrences '.' in f - should be one at location 4
> # but regexpr gives 1 at location 1
> #[1] 1
> #attr(,"match.length")
> #[1] 1
>
> Sorry if I am missing something obvious. I'd be very grateful if someone
> would
> please show me how to use regexpr to locate '.' in my string!
>
> Thanks,
>
> Jonathan Williams
>
> ______________________________________________
> 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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 Tue 15 Apr 2008 - 10:34:21 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 Tue 15 Apr 2008 - 11:30:28 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