
我有這樣的場景:
data-cycle-center-horz=true other words
data-cycle-caption=".custom-captions" other words
data-cycle-caption-template="{{slideNum}} other words
before words .data-cycle-caption-template="{{slideNum}} other words
.data-cycle-caption-template="{{slideNum}} other words
所以我需要找到所有用符號分隔的單字- = . "{
我為NOTEPAD++製作了一個正規表示式來搜尋和刪除那些前後有符號的單字(整個字串)而沒有其他單詞,但不是很好:
搜尋: (?!\w+[-."={])
替換:(留空)
預期結果應該是:
other words
other words
other words
before words other words
other words
答案1
- Ctrl+H
- 找什麼:
(?:^|[+=."{}-]+)(?:\w+[+=."{}-]+)+\h*
- 用。
LEAVE EMPTY
- 檢查環繞
- 檢查正規表示式
- Replace all
解釋:
(?: # start non capture group
^ # beginning of line
| # OR
[+=."{}-]+ # 1 or more + = . " { } -
) # end group
(?: # start non capture group
\w+ # 1 or more word character
[+=."{}-]+ # 1 or more + = . " { } -
)+ # end group, may appear 1 or more times
\h* # 0 or more horizontal spaces
螢幕截圖:
答案2
這是用 Python 編寫的,它從同一目錄中的檔案“data.txt”加載測試數據
import os, re
path = "./data.txt"
if os.path.isfile(path): #use local leader file
oFile = open(path)
strFile = oFile.read() #get old leaders
oFile.close()
else:
print("File Not Found")
def replace(line):
for i in line:
if ( i == '-' or i == '=' or i == '.' or i == '"' or i == '{' or i == '}'):
line = line.replace(i,"\n")#Delete \n and replace it with xyz to see
return line
lines = strFile.split("\n")
for line in lines:
answer = replace(line)
print(answer)
資料循環中心水平真實資料循環標題自訂標題資料循環標題範本slideNum
答案3
我從你的問題中讀到的是,你想要基本上匹配所有單詞,而不是分隔的特殊字符,對吧?
[^-=."{}\r\n]+
應該可以解決問題。它將匹配除特殊字元- = . "{
或換行符之外的所有內容。
您可以使用線上工具建立和測試正規表示式,例如正規表示式101
更新
以下正規表示式刪除您所描述的單字以及尾隨空格:([^\s]+[-=."{}\r\n][^\s]+\s*)+
我在你的例子上成功測試了它:
前:
data-cycle-center-horz=true other words
data-cycle-caption=".custom-captions" other words
data-cycle-caption-template="{{slideNum}} other words
before words .data-cycle-caption-template="{{slideNum}} other words
.data-cycle-caption-template="{{slideNum}} other words
後:
other words
other words
other words
before words other words
other words