Difficulty with "\\" in string functions....

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Difficulty with "\\" in string functions....

Bernard McGarvey
I am using the file.choose() function to choose a file from the dialog box and once I get it, I want to be able to split the full name into the folder part and the file name part. So for example, when I have closed the file choose dialog, the name for the file I get is


Fname1
[1] "D:\\Data\\OneDrive\\ISTA Documents\\QT_App\\QT Analysis Input Data Example WorkBook.xlsx"


where the "\\" is used to split the folder and sub-folder and file names. R see this "\\" as a single \ backslash character.


Now I try to split it using


str_split(Fname1,"\\")


but this returns an error


Error in stri_split_regex(string, pattern, n = n, simplify = simplify, :
Unrecognized backslash escape sequence in pattern. (U_REGEX_BAD_ESCAPE_SEQUENCE)


I know its got something to do with the \\ because it is treated as a single backslash character. But replacing the str_split with


str_split(Fname1,"\")


does not work either.


Any ideas on how I can handle the \\ and split the full name into its pieces?



Lion Bernard McGarvey

Director, Fort Myers Beach Lions Foundation, Inc.

Retired (Lilly Engineering Fellow).



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty with "\\" in string functions....

Ivan Krylov
On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
Bernard McGarvey <[hidden email]> wrote:

> Now I try to split it using
>
>
> str_split(Fname1,"\\")
>
>
> but this returns an error
>
>
> Error in stri_split_regex(string, pattern, n = n, simplify =
> simplify, : Unrecognized backslash escape sequence in pattern.
> (U_REGEX_BAD_ESCAPE_SEQUENCE)

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, "\\\\")
[[1]]
[1] "D:"                                        
[2] "Data"                                      
[3] "OneDrive"                                  
[4] "ISTA Documents"                            
[5] "QT_App"                                    
[6] "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,
Ivan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty with "\\" in string functions....

Bernard McGarvey
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 <[hidden email]> wrote:
>
>
> On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
> Bernard McGarvey <[hidden email]> wrote:
>
> > Now I try to split it using
> >
> >
> > str_split(Fname1,"\\")
> >
> >
> > but this returns an error
> >
> >
> > Error in stri_split_regex(string, pattern, n = n, simplify =
> > simplify, : Unrecognized backslash escape sequence in pattern.
> > (U_REGEX_BAD_ESCAPE_SEQUENCE)
>
> 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, "\\\\")
> [[1]]
> [1] "D:"                                        
> [2] "Data"                                      
> [3] "OneDrive"                                  
> [4] "ISTA Documents"                            
> [5] "QT_App"                                    
> [6] "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,
> Ivan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty with "\\" in string functions....

R help mailing list-2
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"
> basename(Fname1)
[1] "QT Analysis Input Data Example WorkBook.xlsx"
> dirname(Fname1)
[1] "D:/Data/OneDrive/ISTA Documents/QT_App"

Use normalizePath if you need to convert those / to \ on Windows.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Feb 11, 2019 at 12:26 PM Bernard McGarvey <
[hidden email]> 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 <[hidden email]>
> wrote:
> >
> >
> > On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
> > Bernard McGarvey <[hidden email]> wrote:
> >
> > > Now I try to split it using
> > >
> > >
> > > str_split(Fname1,"\\")
> > >
> > >
> > > but this returns an error
> > >
> > >
> > > Error in stri_split_regex(string, pattern, n = n, simplify =
> > > simplify, : Unrecognized backslash escape sequence in pattern.
> > > (U_REGEX_BAD_ESCAPE_SEQUENCE)
> >
> > 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, "\\\\")
> > [[1]]
> > [1] "D:"
> > [2] "Data"
> > [3] "OneDrive"
> > [4] "ISTA Documents"
> > [5] "QT_App"
> > [6] "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,
> > Ivan
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty with "\\" in string functions....

Bernard McGarvey
In reply to this post by Bernard McGarvey
Simple when you know how!

Thanks

Sent from mobile device - please excuse any spelling mistakes.



------ Original Message ------

From: William Dunlap
To: Bernard McGarvey
Cc: Ivan Krylov, [hidden email]
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"
>basename(Fname1)
[1] "QT Analysis Input Data Example WorkBook.xlsx"
>dirname(Fname1)
[1] "D:/Data/OneDrive/ISTA Documents/QT_App"


Use normalizePath if you need to convert those / to \ on Windows.

Bill Dunlap
TIBCO Software
wdunlaptibco.com(http://tibco.com)

On Mon, Feb 11, 2019 at 12:26 PM Bernard McGarvey<[hidden email](mailto:[hidden email])>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<[hidden email](mailto:[hidden email])>wrote:
> >
> >
> >On Mon, 11 Feb 2019 15:01:16 -0500 (EST)
> >Bernard McGarvey<[hidden email](mailto:[hidden email])>wrote:
> >
> >>Now I try to split it using
> >>
> >>
> >>str_split(Fname1,"\\")
> >>
> >>
> >>but this returns an error
> >>
> >>
> >>Error in stri_split_regex(string, pattern, n = n, simplify =
> >>simplify, : Unrecognized backslash escape sequence in pattern.
> >>(U_REGEX_BAD_ESCAPE_SEQUENCE)
> >
> >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, "\\\\")
> >[[1]]
> >[1] "D:"
> >[2] "Data"
> >[3] "OneDrive"
> >[4] "ISTA Documents"
> >[5] "QT_App"
> >[6] "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,
> >Ivan
>
> ______________________________________________
> [hidden email](mailto:[hidden email])mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.