`Hello @john` -> true
`Hello @john ` -> false
`Hello @john.` -> false
`Hello @john i` -> false
回答
嘗試正則表達式:@\p{L}+(?:$|\n)
\p{L} -> Match matches any kind of letter from any language
$ -> Match End of the string
這個正則表達式 - 「@ \ w + $' - 不僅與ASCII字母匹配,因爲ICU速記類支持Unicode。此外,它將匹配字符串末尾的「@_____」。更多的,這個正則表達式將打印* true *作爲像''@Вася\ n「'這樣的字符串。提到後面跟着一個換行符,而不是在字符串的最後。 '@ \ w + $'是**錯誤的解決方案**。 @Ashraful,修復或刪除請。 –
@WiktorStribiżew很好的捕獲。 –
順便說一句,提供鏈接到PCRE演示並不能證明正則表達式的工作原理,regex101.com不支持ICU正則表達式。 –
如果您想驗證用模式的用戶提字符串,您在說明顯示它是最好寫入String
的擴展名。這將驗證數據。
嘗試:
extension String {
func mention() -> Bool {
let pattern = "@[a-zA-Z]+$"
guard let _ = self.range(of:pattern, options: .regularExpression) else {
return false
}
return true
}
}
測試用例:
let input = ["Hello @john", "Hello @john ", "Hello @john.", "Hello @john i,", "@_____", "@Вася\n"]
for item in input {
if !item.mention() {
print("Failed to get mention at | \(item) |")
}
}
驗證:
Failed to get mention at | Hello @john |
Failed to get mention at | Hello @john. |
Failed to get mention at | Hello @john i, |
Failed to get mention at | @_____ |
Failed to get mention at | @Вася
|
- 1. Twitter iOS API是否提供了確定推文是否成功的方法?
- 2. Cassandra JDBC是否提供了一個DataSource?
- 3. C#.Net如何確定是否提供了可以爲空的DataMember值
- 4. 是否明確提供了可能在Scala中的上下文綁定?
- 5. 在AngularJS中,如何確定屬性是否提供給組件?
- 6. 如何確定VFPOLEDB提供程序是否已安裝/註冊?
- 7. Roku是否向文本提供語音?
- 8. C++程序 - 不確定是否我提供了正確的解決方案
- 9. 如何確定是否啓用了CONFIG_FANOTIFY_ACCESS_PERMISSIONS?
- 10. 如何確定表單是否提升了其顯示事件
- 11. 如何確定文件是否是Eclipse中的文本文件
- 12. retainCount是否爲我提供了我的NSDate的正確信息?
- 13. DynamoDB是否爲此用例提供了正確的選項?
- 14. RequireJS是否提供了依賴API?
- 15. Perl中是否提供了POSIX pselect?
- 16. 在emguCV中是否提供了cvCreateTrackbar?
- 17. 是否在WPF中提供了RecreateDatabaseIfModelChanges?
- 18. myBatis是否提供了一些方法,例如refreshAndLock?
- 19. 如何確定我是否安裝了本地SQL Server 2008 R2
- 20. 如何確定渲染是否完成了反應本機
- 21. iPhone是否提供了一個很好的加密API /服務提供者?
- 22. 是否有一個Python庫,它爲給定的unicode字符或字符串提供了腳本名稱?
- 23. 如何檢查函數調用中是否提供了參數?
- 24. 我如何知道Geocoder是否提供了android?
- 25. 如何確定一個字符串是否是ksh的數字
- 26. 確定字符串是否是文本中的專有名詞
- 27. 確定文本是否爲英文?
- 28. 確定文本是否爲英文
- 29. 如何測試一個類是否明確定義了__gt__?
- 30. 如何確定一個元素是否配置了對話框?
有什麼問題嗎?你能詳細說明一下嗎? –
您可以通過提及請詳細說明您的意思 – RiggsFolly