[R] Regexpr with "."

Stephen C. Upton upton at mitre.org
Wed Aug 13 17:11:31 CEST 2003


Trevor,

The "." is a regex meta-character that matches any character. In order to look
specifically for a ".", the you must escape it with a "\", and that "\" must
also be escaped, thus,

> regexpr("\\.", "Female.Alabama")
[1] 7
attr(,"match.length")
[1] 1
>

HTH
steve

"Thompson, Trevor" wrote:

> I'm trying to use the regexpr function to locate the decimal in a character
> string.  Regardless of the position of the decimal, the function returns 1.
> For example,
>
> > regexpr(".", "Female.Alabama")
> [1] 1
> attr(,"match.length")
> [1] 1
>
> In trying to figure out what was going on here, I tried the below command:
>
> > gsub(".", ",", "Female.Alabama")
> [1] ",,,,,,,,,,,,,,"
>
> It looks like R is treating every character in the string as if it were
> decimal.  I didn't see anything in the help file about "." being some kind
> of special character.  Any idea why R is treating a decimal this way in
> these functions?   Any suggestions how to get around this?
>
> Thanks for any suggestions.
>
> -Trevor
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list