'\w' is an unrecognized escape in grep
You need to escape the backslashes one more time in r.
d$SomeColumn[grep("(?ix)<VNW[^;]*;(dis|dat)> \\w*<N\\(", d$Right, perl=TRUE)] <- 1
| |
Is it possible to escape [ in R
By default grep()
uses regular expressions and [
is a special character in regular expression, you can either disable regular expressions with
grep("[", a, fixed=TRUE)
or escape the [
by doing
grep("\\[", a)
Note the double slash here because the correct regular expression syntax would be \[
but you also need to escape the slash in the R string since you want a literal slash and not an escape code so it becomes \\[
An error ['\+' is an unrecognized escape in character string starting \+ while creating a R package
The offending code comes from the Examples section of one of your help files (which is why it ends up in packageName-Ex.R
). To fix it, just escape each of the backslashes in the Examples sections of your *.Rd
documentation files with a second backslash. (So, type \\
to get \
in the processed help file, and type \\\\
to get \\
.)
Unless it's escaped, \
is interpreted as a special character that identifies sectioning and mark-up macros (i.e. commands like \author
, \description
, \bold
, and \ldots
). Quoting from Duncan Murdoch's Parsing Rd files (the official manual for this topic):
The backslash \ is used as an escape character: \, \%, { and }
remove the special meaning of the second character.
As an example of what this looks like in practice, here is part of $R_SOURCE_HOME/src/library/base/man/grep.Rd
, which is processed to create the help file you see when you type ?grep
or ?gsub
.
## capitalizing
txt <- "a test of capitalizing"
gsub("(\\\\w)(\\\\w*)", "\\\\U\\\\1\\\\L\\\\2", txt, perl=TRUE)
gsub("\\\\b(\\\\w)", "\\\\U\\\\1", txt, perl=TRUE)
In the processed help file, it looks like this:
## capitalizing
txt <- "a test of capitalizing"
gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", txt, perl=TRUE)
gsub("\\b(\\w)", "\\U\\1", txt, perl=TRUE)
Regex Failing Unrecognized Escape sequence
The problem here is, you want to escape for two different levels. The \A
is a escape sequence for the regex. But the issue is, there is at first the string that tries to interpret escape sequences and the string does not know the escape sequence \A
or \s
(I don't know).
Two solutions are possible:
if you are escaping for the regex, double the
\
. Sovar module107 = Regex("\\A*Module\\sID=\"107\"");
is the string and after the string is processed, the regex is
\A*Module\sID="107"
Use verbatim strings. If you add a
@
before the string, escape sequences are not evaluated by the string. SoRegex(@"\A*Module\sID=")
would end as regex\A*Module\sID=
But now you are getting problems with the
"
you want to have in the regex. You can add a"
to a verbatim string by doubling it:var module107 = Regex(@"\A*Module\sID=""107""");
Extract word infront of certain character RegEx Rstudio
Using sub
sub(".*as\\s(\\w+).*", "\\1", "sam buy expensive toys as 125898652")
#[1] "125898652"
Or lookbehind regex
stringr::str_extract("sam buy expensive toys as 125898652", "(?<=as\\s)\\w+")
#[1] "125898652"
For words which has ,
in it and may have decimal places we can do
x <- "sam buy expensive toys as 128984,45697.00"
sub(".*as\\s(\\d+\\.?\\d+).*", "\\1",gsub(',', '', x))
#[1] "12898445697.00"
warning: unknown escape sequence '\
In C string literals the \
has a special meaning, it's for representing characters such as line endings \n
. If you want to put a \
in a string, you need to use \\
.
For example
"\\Hello\\Test"
will actually result in "\Hello\Test".
So your regexp needs to be written as:
"[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\}\\\\.[0-9]\\{1,3\\}\\\\.[0-9]\\{1,3\\}"
instead of:
"[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}\\.[0-9]\{1,3\}"
Sure this is painful because \
is used as escape character for the regexp and again as escape character for the string literal.
So basically: when you want to put a \
you need to write \\
.
Related Topics
Ggpairs Plot with Heatmap of Correlation Values
List Members Can Be Accessed with Partial Name? Is This a Feature
Dplyr Rowwise Sum and Other Functions Like Max
Combinations of Multiple Vectors in R
Align Two Data.Frames Next to Each Other with Knitr
Find All Combinations of Numbers That Sum to a Target
Use Rollapply and Zoo to Calculate Rolling Average of a Column of Variables
Using Dplyr Within a Function, Non-Standard Evaluation
Count Every Possible Pair of Values in a Column Grouped by Multiple Columns
Add Column Containing Data Frame Name to a List of Data Frames
Two Y-Axes with Different Scales for Two Datasets in Ggplot2
Convert Roman Numerals to Numbers in R
As.Date(As.Posixct()) Gives the Wrong Date
Trying to Merge Multiple CSV Files in R
Automated Httr Authentication with Twitter , Provide Response to Interactive Prompt in "Batch" Mode