Search
 
SCRIPT & CODE EXAMPLE
 

R

regex in r

^ = Beginning of Line or String
$ = End of Line or String
. = Matches Anything like a Joker Card (inc blank spaces)
. = Escape the period when we search on an actual period

d or [::digit::] = 0, 1, 2, 3, ...
w or [::word::] = a, b, c, ..., 1, 2, 3, ..., _
[A-Za-z] or [::alpha::] = A, B, C, ... a, b, c, ...
[aeiou] = a, e, i, o, u
s or [::space::] = " ", tabs or line breaks

D = match all except digits
W = match all except word char (inc numbers and underscore)
S = match all except spaces, tabs or line breaks
[^A-Za-z] = match all except alphabet

d{2} = repeat digit exactly twice (double digit)
d{2, 3} = min repeat digit twice, max repeat 3 thrice
d{2,} = min repeat digit twice, no max 
d+ = 1 or more repetitions 
d* = 0, 1 or more repetitions 

| = OR (pattern = "apple|apples") [Not limited to 2 options only]
# No spaces are allowed within the vertical bar(s)
? = make the preceding group or character optional or
make the preceding multiplier "lazy" instead of "greedy".
Similar as above when coded as (pattern = apples?)

Greedy and Lazy [See Example 5a and b]
pattern = ".*3" (match everything before the last digit of 3)[Greedy]
pattern = ".*?3" (match only everything before the first digit of 3)[Lazy]

Examples:
1) Find all digits and spaces
stringr::str_match_all(x, pattern = "[ds]")

2) List all strings that end with a space followed by a digit
data[stringr::str_detect(x, pattern = "sd$")]

3) List all strings that contain "Grey" or "Gray"
data[stringr::str_detect(x, pattern = "Gr[ae]y")]

4) List all strings with strange characters (no word or space)
data[stringr::str_detect(x, pattern = "[^ws]")]

5) Greedy vs Lazy
	a) Greedy
	   >stringr::str_match("a 3 b 3 c", ".*3")
	   "a 3 b 3"
	b) Lazy
		>stringr::str_match("a 3 b 3 c", ".*?3")
		"a 3"

6) Three or more word characters, followed by an at-sign, 
one or more word characters and a ."com".
pattern = "w{3,}@w+.com"
Comment

regex in R

x <- "1888 is the longest year in Roman numerals: MDCCCLXXXVIII"
str_extract(x, "CC?")
#> [1] "CC"
str_extract(x, "CC+")
#> [1] "CCC"
str_extract(x, 'C[LX]+')
#> [1] "CLXXX"
Comment

PREVIOUS NEXT
Code Example
R :: how to read csv file in r 
R :: ggplot2 font times new roman 
R :: Getting rid of row names in R 
R :: histogram r add line 
R :: create a table from dataframe in r 
R :: convert a matrix to a vector in r 
R :: r type of object 
R :: R dplyr select 
R :: reorder factors in r 
R :: repeat sample in r 
R :: covert -Inf to 0 r 
R :: remove_all_labels(x) 
R :: infinite in r 
R :: r select rows 
R :: run regression for certain groups in r 
R :: extract hyperlinks in r 
R :: link excel to r 
R :: save large nested list to text R 
R :: interquartile in r 
R :: save link tweet in new column in R 
R :: same plots for every variable together 
R :: r count rows dataframe 
Rust :: how to create a string of n characters rust 
Rust :: rust read lines from stdin and return a vec 
Rust :: rust lang rand between 
Rust :: split rust 
Rust :: rust modulus 
Rust :: armanriazi•rust•error•[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable 
Rust :: armanriazi•rust•concept•oop•state•pattern 
Rust :: rust•armanriazi•let•const 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =