[R] Regular Expression returning unexpected results
David Carlson
dcarlson at tamu.edu
Tue Oct 29 20:05:38 CET 2013
>From ?regex
"(do remember that backslashes need to be doubled when entering
R character strings, e.g. from the keyboard)."
> lines[grep("^([a-z]+) +\\1 +[a-z]+ [0-9]",lines)]
[1] "night night at 8"
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Lopez, Dan
Sent: Tuesday, October 29, 2013 12:13 PM
To: R help (r-help at r-project.org)
Subject: [R] Regular Expression returning unexpected results
Hi,
So I just took an intro to R programming class and one of the
lectures was on Regular Expressions. I've been playing around
with various R functions that use Regular Expressions.
But this has me stumped. This was part of a quiz and I got it
right through understanding the syntax. But when I try to run
the thing it returns 'integer(0)'. Can you please tell me what I
am doing wrong?
#I copied and pasted this:
going up and up and up
night night at 8
bye bye from up high
heading, heading by 9
#THEN
lines<-readLines("clipboard")
#This is what it looks like in R
lines
[1] "going up and up and up"
[2] "night night at 8"
[3] "bye bye from up high"
[4] "heading, heading by 9"
#THIS IS WHAT IS NOT WORKING THE WAY I THOUGHT. I was expecting
it to return 2.
# "night night at 8" follows the pattern: Begins with a word
then has at least one space then the same word then has at least
one space then a word then a space then a single digit number.
grep("^([a-z]+) +\1 +[a-z]+ [0-9]",lines)
integer(0)
#But simple examples DO work
grep("[Hh]",lines)
[1] 2 3 4
grep('[0-9]',lines)
[1] 2 4
[[alternative HTML version deleted]]
______________________________________________
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.
More information about the R-help
mailing list