我需要構建一個正則運算式來捕獲文本中的一個或多個視窗路徑。它用于語法熒光筆。
想象一下這段文字:
Hey, Bob!
I left you the report for tomorrow in D:\Files\Shares\report.pdf along
with the other reports.
There's also this pptx here D:\Files\Internal\source.pptx where you have
the original if you need to change anything.
Cheers!
Alice.
這個很容易捕捉到/[a-zA-Z]:\\[^\s]*/mg。在此處的 regex101 中查看
盡管如此
當路徑有空格時:
I left you the report for tomorrow in D:\Shared files\october report.pdf along
with the other reports.
那么我們就會遇到問題:路徑是什么?D:\Shared或D:\Shared files\october或D:\Shared files\october report.pdf或D:\Shared files\october report.pdf along...
對于人類來說,推斷很簡單。對于計算機來說這是不可能的,所以我想強迫用戶使用引號或括號來指示檔案名或路徑的開始和結束。
問題
鑒于此,我該如何撰寫正則運算式:
Hey, Bob!
I left you the report for tomorrow in "D:\Shared files\october report.pdf" along
with the other reports [Don't forget to add your punctuation]. See
also D:\Multifiles\charlie.docx for more info.
There's also this pptx here [D:\Internal files\source for report.pptx] where you have
the original if you need to change "anything like the boss wants".
Cheers!
Alice.
捕捉到這個?
D:\Shared files\october report.pdf
D:\Multifiles\charlie.docx
D:\Internal files\source for report.pptx
但不是
Don't forget to add your punctuation
anything like the boss wants
非作業樣本:
PD:靈感來自這里https://es.javascript.info/regexp-lookahead-lookbehind
uj5u.com熱心網友回復:
如果您有類似".." [..]或僅匹配非空白字符的邊界:
\b(?:(?<=\[)[a-z]:\\[^]]*(?=])|(?<=")[a-z]:\\[^"]*(?=")|[a-z]:\\\S )
模式匹配:
\b防止部分單詞匹配的單詞邊界(?:非捕獲組(?<=\[)[a-z]:\\[^]]*(?=])在[...]匹配 az之間:\,然后匹配可選字符,而不是]使用否定字符類|或者(?<=")[a-z]:\\[^"]*(?=")在"..."匹配 az之間:\,然后匹配可選字符,而不是"使用否定字符類|或者[a-z]:\\\S匹配 az:\然后 1 非空白字符
)關閉非捕獲組
查看正則運算式演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529591.html
