3 回答

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用包含 3 個(gè)數(shù)字或一系列非數(shù)字的交替組,前面有一個(gè)單詞邊界斷言:
^.*?\b(?:\d{3}|\D+)\.\w{3}$
演示:https ://regex101.com/r/A9iSVE/3

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
為了將您的要求考慮到逗號和雙空格,一種選擇可能是使用 2 個(gè)負(fù)前瞻來斷言字符串不包含雙空格并且在逗號之前不包含空格。
\s
如果要匹配空白字符而不是單個(gè)空格,則可以使用。
^(?!.*[ ]{2})(?!.* ,).*\b(?:\p{L}+|\d{3})\.\w{3}$
那將匹配
^
字符串的開始(?!.*[ ]{2})
斷言不是 2 個(gè)空格(?!.* ,)
斷言不是一個(gè)空格和一個(gè)逗號.*\b
匹配任何字符 0+ 次,后跟單詞邊界(?:\p{L}+|\d{3})
匹配 1+ 次字母或 3 位數(shù)字\.\w{3}
匹配.
和 3 個(gè)單詞字符$
字符串結(jié)束

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以在不回溯的情況下滿足指定的規(guī)則(當(dāng)前接受的答案就是這樣)。指定的規(guī)則是(為了清楚起見重新措辭):文件名必須滿足以下條件:
它不得包含多個(gè)空格字符的序列。
逗號后面必須有一個(gè)空格字符。
文件名詞干可以有一個(gè) 3 位數(shù)的后綴。
文件擴(kuò)展名必須由 3 個(gè)字母組成。
為此:
^(?<prefix>[^, ]+(,? [^, ]+)*)(?<suffix>\d\d\d)?(?<extension>.\p{L}\p{L}\p{L})$
會成功的,沒有花哨的前瞻,沒有回溯。分解成碎片,你會得到:
^ # * match start-of-text, followed by
(?<prefix> # * a named group, consisting of
[^,\x20]+ # * 1 or more characters other than comma or space, followed by
( # * a group, consisting of
,? # * an optional comma, followed by
\x20 # * a single space character, followed by
[^,\x20]+ # * 1 or more characters other than comma or space
)* # with the whole group repeated zero or more times
) # followed by
(?<suffix> # * an optional named group (the suffix), consisting of
\d\d\d # * 3 decimal digits
)? # followed by
(?<extension> # * a mandatory named group (the filename extension), consisting of
.\p{L}\p{L}\p{L} # * 3 letters.
) # followed by
$ # end-of-text
- 3 回答
- 0 關(guān)注
- 361 瀏覽
添加回答
舉報(bào)