Skip to content

Instantly share code, notes, and snippets.

@turgeonmaxime
Created March 15, 2024 15:18
Show Gist options
  • Save turgeonmaxime/1eda291af4f840bbdb65ff4c97fc7bd0 to your computer and use it in GitHub Desktop.
Save turgeonmaxime/1eda291af4f840bbdb65ff4c97fc7bd0 to your computer and use it in GitHub Desktop.
Extract some filename annotations using regex
library(stringr)
data_path <- 'somefolder/planet04_rgbn_c9_r8.txt'
# We want to extract 9 and 8
basename(data_path)
# Too many numbers
str_extract_all(basename(data_path), "([0-9]+)")
# [[1]]
# [1] "04" "9" "8"
# Right numbers, but don't want _c and _r
str_extract_all(basename(data_path), "(_c|_r)([0-9]+)")
# [[1]]
# [1] "_c9" "_r8"
# Right number, missing 8
str_extract_all(basename(data_path), "(?<=c)\\d+")
# [[1]]
# [1] "9"
# Correct solution
str_extract_all(basename(data_path), "(?<=(_c|_r))\\d+")
# [[1]]
# [1] "9" "8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment