praw: Pattern (or Pseudo) raw: Approximate Python-like 'raw'...

Description Usage Arguments Details Value Examples

Description

Uses 'backtick' (or another character or string) as an alias for 'backslash' in regex pattern strings,

Usage

1
praw(pat, cesc = "`")

Arguments

pat

a string or character vector

cesc

a character or string (default ') to be substituted by double \ in the returned pattern string where any cescs appeared on input.

Details

Literal occurrences of characters such as ([.|)\^+$*?:] often need to be escaped in regular expression patterns. This may make them look very different from, e.g., Python raw string patterns. Thus, the equivalent of Python's pyPat= r'\(\s*\w+\s*\)' would have to look like: rPat <- "\\(\\s*\\w+\\s*\\)". With praw() we can instead use erPat <- ('`(`s*`w+`s*`)') To match a literal single backslash in a string, pat <- praw('``') is necessary, generating the same string as pat= '\\'. To match a double literal backslash R would require an eight 'leaning toothpicks' pattern ('\\\\\\\\'), but with praw, we get away with four backticks pat <- praw('````'), yielding \\\\\\\\. If a literal backtick is in the pattern of interest, you can substitute another unused character to stand in for \\ sequences.

Value

the pattern with the 'cesc' character replaced by two backslashes. See details.

Examples

1
2
3
4
5
6
7
praw('`(`s*`w+`s*`)') == "\\(\\s*\\w+\\s*\\)"

# Replace a\\b with a:b

#' gsub(praw("````"), ":", "a\\\\b")
# or  using '%' instead of `'`:
gsub(praw('%%%%','%'), ":", "a\\\\b")

tnearey/nutils documentation built on May 8, 2019, 12:50 p.m.