[英]Regular Expression for tackling special symbols
I have an example here : 我这里有一个例子:
>>> txt1
'fdf\\.\\..dgg'
I intend to find a regex that will return me the special symbols . 我打算找到一个正则表达式,它将给我返回特殊符号。
So i tried this . 所以我尝试了这个。
>>> ans=re.search("\w+[\|.]*\w+",txt1)
>>> ans.group()
'fdf'
The \\w+ will find words continuing. \\ w +会发现单词继续。 The [\\|.] was supposed to find \\ or .
[\\ |。]应该找到\\或。 (dot) .
(点)。 The star was supposed to continue for next entry.
该明星本应继续下一次进入。 Again, the \\w+ was supposed to find trailing words.
同样,\\ w +应该找到结尾的单词。
Please guide what is wrong here? 请指导这里有什么问题? Or the concept is not exactly what i think it is ... Thanks in advance to all.... As you can see , the idea is not working .
还是这个概念与我想的不完全相同。...在此先感谢所有人。...如您所见,这个想法行不通。
"I intend to find a regex that will return me the special symbols." “我打算找到一个能返回特殊符号的正则表达式。”
re.search(r"\w+([\\\.]*)\w+", txt1)
finds with ans.group(1)
what you need: 使用
ans.group(1)
找到您需要的东西:
ans = re.search(r"\w+([\\\.]*)\w+", txt1)
ans.group(1)
# '\\.\\..'
The []
designs a group of characters (without the |
"or"), but you have to escape the backslash and dot with backslash \\. -> \\\\\\.
[]
设计一组字符(不带|
“或”),但是您必须转义反斜杠,并用反斜杠\\. -> \\\\\\.
点号\\. -> \\\\\\.
\\. -> \\\\\\.
to match it. 匹配它。
如果您想在其中找到任何非字母数字(包括空格)的内容,请使用:
[^\w]+
You can't use the alternation operator |
您不能使用交替运算符
|
in a character class. 在角色类中。 Inside
[ ]
a pipe stands for exactly that character. 在
[ ]
,管道代表该字符。 Your backslash escapes it (unnecessarily) so you are looking for pipes or dots. 您的反斜杠会将其转义(不必要),因此您正在寻找竖线或圆点。 What you want is
你想要的是
ans=re.search(r"\w+[\\.]*\w+", txt1)
Since you want to find special symbols, re.findall(r"[az]*([.\\\\] ?)[az]*", txt1)
will return your symbols as a list. 由于要查找特殊符号,因此
re.findall(r"[az]*([.\\\\] ?)[az]*", txt1)
会将您的符号作为列表返回。 You can always join()
them as needed (example shown below): 您始终可以根据需要
join()
它们(如下所示的示例):
>>>
>>> txt1
'fdf\\.\\..dgg'
>>> ans = re.findall(r"[a-z]*([.\\] ?)[a-z]*", txt1)
>>> ans
['\\', '.', '\\', '.', '.']
>>>
>>> "".join(ans)
'\\.\\..'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.