[R] Difficulty with "\\" in string functions....
mcg@rvey@bern@rd @end|ng |rom comc@@t@net
Mon Feb 11 22:33:59 CET 2019
Simple when you know how!
Sent from mobile device - please excuse any spelling mistakes.
------ Original Message ------
From: William Dunlap
To: Bernard McGarvey
Cc: Ivan Krylov, r-help using r-project.org
Sent: February 11, 2019 at 4:29 PM
Subject: Re: [R] Difficulty with "\\" in string functions....
You can also avoid the issue by using the basename and dirname functions.
>Fname1<- "D:\\Data\\OneDrive\\ISTA Documents\\QT_App\\QT Analysis Input Data Example WorkBook.xlsx"
 "QT Analysis Input Data Example WorkBook.xlsx"
 "D:/Data/OneDrive/ISTA Documents/QT_App"
Use normalizePath if you need to convert those / to \ on Windows.
On Mon, Feb 11, 2019 at 12:26 PM Bernard McGarvey<mcgarvey.bernard using comcast.net(mailto:mcgarvey.bernard using comcast.net)>wrote:
> Brilliant! Thanks a million Ivan.
> Lion Bernard McGarvey
> Director, Fort Myers Beach Lions Foundation, Inc.
> Retired (Lilly Engineering Fellow).
> >On February 11, 2019 at 3:13 PM Ivan Krylov<krylov.r00t using gmail.com(mailto:krylov.r00t using gmail.com)>wrote:
> >On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
> >Bernard McGarvey<mcgarvey.bernard using comcast.net(mailto:mcgarvey.bernard using comcast.net)>wrote:
> >>Now I try to split it using
> >>but this returns an error
> >>Error in stri_split_regex(string, pattern, n = n, simplify =
> >>simplify, : Unrecognized backslash escape sequence in pattern.
> >This happens because the second parameter of str_split is by default a
> >regular expression, and a backslash has a special meaning in regular
> >expressions: when preceding other characters, it may change the way
> >they are interpreted. (For example, w means a literal "w"
> >character, while \w means "any alphanumeric character". On the
> >other hand, [ starts a character group, but \[ means just an opening
> >square bracket.) See ?regex for more info on that.
> >Since you want a literal backslash, you need to escape it with another
> >backslash: \\
> >But to write a string literal of a double-backslash in R, you need to
> >escape both backslash characters, each with their own backslash: "\\\\"
> >## fname<- "D:\\Data\\OneDrive\\ISTA Documents\\QT_App\\QT Analysis
> >Input Data Example WorkBook.xlsx"
> >## message("\\\\")
> >## str_split(fname, "\\\\")
> > "D:"
> > "Data"
> > "OneDrive"
> > "ISTA Documents"
> > "QT_App"
> > "QT AnalysisInput Data Example WorkBook.xlsx"
> >You can also avoid all layers of the backslash hell (except the first)
> >if you choose to split by fixed strings instead of regular expressions
> >by using stringr::fixed:
> >## str_split(fname, fixed("\\"))
> >Best regards,
> R-help using r-project.org(mailto:R-help using r-project.org)mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help