regex


grep to match a certain number sequence in a mix of text and numbers


I have several files similar to "PRISM_ppt_stable_4kmM2_------_bil.bil", where ------ is a year and month, like 190112. The file date ranges from 189501 to 198012. In R, on a Windows 7 machine, I would like to match all the files for the time from 192001 to 193912. I'm pretty sure I want grepl(), but I can't figure out the way to refer to the sequence in the command. I tried
my.files[grepl('PRISM.*/1920/.bil$',my.files)]
and
my.files[grepl('PRISM.*[1][9][2][0].',my.files)]
and other variations, but just get error messages. I know that [0-9]{4} will match any four number sequence, but that would match all the files.
Here's what I'd do:
# Reproducible example of file list
library(stringr)
ym <- paste0(1895:1980, str_pad(1:12, 2, pad='0'))
file_list <- paste0("PRISM_ppt_stable_4kmM2_", ym, "_bil.bil")
# Create a list of desired dates and convert to your date format
dates <- seq(as.Date('1920-01-01'), as.Date('1939-12-01'), by='month')
dates <- format(dates, '%Y%m')
# Subset the file list
your_files <- file_list[str_extract(file_list, '[0-9]{6}') %in% dates]

Related Links

Find simplest regular expression matching all given strings
Regex.Split string on each literal (included in result)
vimrc to detect remote connection
Strange issue with regex matching in perl, alternate attempts match
REGEX reformatting
GPA regex in Perl
Replace string unless between two points
TCL passing lists of regexes through command line
Match a Regular Expression by simple 2 cases:
Regex ignore Find and Replace in Notepad++
Perl: How to match a string that is not in a given string [duplicate]
glob2rx in R to get all cells whose last decimal is 5?
Finding file names without a specified character
Perl: quick replacing of occurrences of multiple words in an array
Extract resolution from string
How do I create a Scala Regex that is compiled using Java Pattern.COMMENTS?

Categories

HOME
ssis
asp.net-identity
logstash
load
spring-roo
xquery
antlr4
encog
httpbuilder-ng
microsoft-dynamics
clion
xorg
jscrollpane
pyfftw
python-2.5
export-to-excel
ssrs-tablix
factory-girl
html-email
abaqus
slime
ormlite-servicestack
ssms-2014
bitcode
precision
iteration
epicorerp
azure-redis-cache
weather-api
grouping
code-signing
flatbuffers
expression-trees
true-type-fonts
charts.js
vrtk
azure-app-service-envrmnt
solid-principles
consistency
polymer-cli
easy-digital-downloads
using
tern
permgen
quicksand
jquery-ui-dialog
android-studio-2.1
sdcc
kendo-chart
orchestrate
koala-framework
soql
masm32
master-data-management
waf
masspay
kombu
plotrix
informatica-cloud
ipopt
mink
proc
ellipsis
geneos
angularjs-filter
aplpy
pakyow
asp.net-authorization
uac
coldfusion-10
uat
cng
multiscreen
wss4j
maven-cargo
scalariform
spiceworks
information-schema
physx
bitrock
mpj-express
team-explorer-everywhere
bunny
wmv
yourls
azimuth
windows-phone-7.1
confirmation
android-lru-cache
libnids
adlds
route-me
cassette
harvest
cellid
azure-role-environment
xmltextwriter
openfeint
web-based

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App