Hi, I have a simple question. Suppose I have a string "x$Expensive". I want to find the position of the $ in this string; i.e., I want a function that returns 2. I tried grep, regexpr, etc with no luck, unless I'm just using them incorrectly. Any suggestions? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.com
HI, str1<-"x$Expensive" regexpr("\\$",str1)[1] #[1] 2 ?str2<-"x$Exp$Expression" unlist(gregexpr("\\$",str2)) #[1] 2 6 A.K. ----- Original Message ----- From: Data Analytics Corp. <walt at dataanalyticscorp.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Monday, January 7, 2013 4:22 PM Subject: [R] pattern matching Hi, I have a simple question.? Suppose I have a string "x$Expensive". I want to find the position of the $ in this string; i.e., I want a function that returns 2.? I tried grep, regexpr, etc with no luck, unless I'm just using them incorrectly.? Any suggestions? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.com ______________________________________________ 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.
On Jan 7, 2013, at 3:22 PM, Data Analytics Corp. <walt at dataanalyticscorp.com> wrote:> Hi, > > I have a simple question. Suppose I have a string "x$Expensive". I want to find the position of the $ in this string; i.e., I want a function that returns 2. I tried grep, regexpr, etc with no luck, unless I'm just using them incorrectly. Any suggestions? > > Thanks, > > WaltThe problem with this specific example is that '$' is a metacharacter in regular expressions, so you have to escape it. For example:> regexpr("\\$", "x$Expensive")[1] 2 attr(,"match.length") [1] 1 attr(,"useBytes") [1] TRUE See ?regex for more information and if appropriate, consider gregexpr():> gregexpr("\\$", "x$Expensive$MoreText")[[1]] [1] 2 12 attr(,"match.length") [1] 1 1 attr(,"useBytes") [1] TRUE Regards, Marc Schwartz
"$" has a special meaning (end-of-string) in regular expressions, so you can either escape it with "\\" or not use regular expressions in regexpr():> regexpr("\\$", "x$Expensive")[1] 2 attr(,"match.length") [1] 1 attr(,"useBytes") [1] TRUE> regexpr("$", "x$Expensive", fixed=TRUE)[1] 2 attr(,"match.length") [1] 1 attr(,"useBytes") [1] TRUE Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Data Analytics Corp. > Sent: Monday, January 07, 2013 1:22 PM > To: r-help at R-project.org > Subject: [R] pattern matching > > Hi, > > I have a simple question. Suppose I have a string "x$Expensive". I want > to find the position of the $ in this string; i.e., I want a function > that returns 2. I tried grep, regexpr, etc with no luck, unless I'm > just using them incorrectly. Any suggestions? > > Thanks, > > Walt > > ________________________ > > Walter R. Paczkowski, Ph.D. > Data Analytics Corp. > 44 Hamilton Lane > Plainsboro, NJ 08536 > ________________________ > (V) 609-936-8999 > (F) 609-936-3733 > walt at dataanalyticscorp.com > www.dataanalyticscorp.com > > ______________________________________________ > 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.