簡體   English   中英

正則表達式,用於查找以任何字母開頭和數字開頭的所有文件

[英]RegEx for finding all files that start with any letter followed by a number

我正在嘗試使我的bash腳本更智能。 我有一些代碼正在清除10000個文件。 我要解決的問題是如何找到所有在0901 *前面帶有一串字母az或AZ的文件。*

find . -maxdepth 1 -type f -regextype posix-extended -regex '[a-z]\(0901).*'

find . -maxdepth 1 -type f -regextype posix-extended -regex '[az]*.*' find . -maxdepth 1 -type f -regextype posix-extended -regex '[az]*.*'返回所有內容

一會兒find . -maxdepth 1 -type f -regextype posix-extended -regex '[az]\\(0901).*' find . -maxdepth 1 -type f -regextype posix-extended -regex '[az]\\(0901).*'返回任何內容。

我該如何解決這個問題?

以下選項應與sed用作正則表達式引擎:

find . -maxdepth 1 -type f -regextype sed -regex "[A-Za-z]\+0901.*"

我將a bunch of letters解釋為一個或多個字母,因此我在數字前使用了[A-Za-z]+ 這里不需要括號,但是如果要使用括號,則必須通過反斜杠將其轉義:

find . -maxdepth 1 -type f -regextype sed -regex "[A-Za-z]\+\(0901\).*"

使用-regex返回整個路徑(視情況而定),需要考慮./。 因此,大量借鑒了蒂姆的答案。

find . -maxdepth 1 -type f -regextype sed -regex '.*[A-Za-z]\\+\\(0901\\).*'

我認為您必須使用此正則表達式:

"[A-Za-z]0901"

祝好運!

暫無
暫無

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

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