[英]Python regular expression for String starting with tr(“ and ending with ”)
[英]Python regular expression for words not starting and not ending with
我正在嘗試為以下規則在pythond中構造正則表達式,
:)
(請考慮這兩個) 例如。
接受單詞
Hello
Hello-World
Hello_
Hello1
拒絕單詞
_hello_
hello:
hello:)
我想出了以下正則表達式,
'(?!_)[\w-]+(?!:)'
它仍然接受所有單詞,只是在統計信息中跳過_,而在末尾跳過:,
有人可以指出,我的正則表達式怎么了謝謝
您可以添加\\b
開頭和結尾。
words = ["Hello", "Hello-World", "Hello_", "Hello1", "_hello_", "hello:",
"hello:)" ]
import re
for word in words:
print re.findall(r'\b(?!_)[\w-]+(?!:)\b', word)
輸出:
['Hello']
['Hello-World']
['Hello_']
['Hello1']
[]
[]
[]
來自http://docs.python.org/2/library/re.html
\\b
匹配空字符串,但僅匹配單詞的開頭或結尾。 單詞定義為字母數字或下划線字符的序列,因此單詞的結尾由空格或非字母數字的非下划線字符指示。
您的要求仍然有很多歧義,但這是您在給小提琴准備的樣本集的另一種解決方案
^[A-Za-z-]+[_\d]?$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.