簡體   English   中英

正則表達式,用於查找與R中的模式匹配的文件

[英]Regular expression to find files that match a pattern in R

我在R中使用正則表達式遇到了一些困難

我正在尋找符合以下模式的所有文件:

文件名應以“11”開頭,以“.JPG”結尾

我應該使用什么正則表達式?

list.files(path='my_path', pattern=???)

謝謝

您可以使用^來表示“在開始時”, $表示“在結尾”,而.*表示中間的所有內容,因此您可以嘗試以下方法:

list.files(path='my_path', pattern="^11.*\\.JPG$")

嘗試這個小實驗,看看每個模式的結果如何不同:

someFiles <- c("testpost.html", "mytest.html", "testing.html", "testing.txt")
grep("test", someFiles)
grep("^test", someFiles)
grep("\\.txt", someFiles)
grep("^test.*\\.html", someFiles)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM