我有这种情况:
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)
数据循环中心 horz 真实数据循环标题自定义标题数据循环标题模板 slideNum
答案3
我从你的问题中读出的是你想要基本上匹配所有的单词,但不匹配分隔的特殊字符,对吗?
[^-=."{}\r\n]+
应该可以解决问题。它将匹配除特殊字符- = . "{
或换行符之外的所有内容。
您可以使用以下在线工具构建和测试正则表达式:regex101
更新
以下正则表达式将删除您描述的单词以及尾随空格:([^\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