英文:
Regex to control input template 2numbers-space-2letters-space-3numbers
问题
I need regex that controls "00 XX 000 " pattern
please help
private fun isTransferDescriptionValid(description: String): Boolean {
val regex = "^[a-zA-Z0-9] [a-zA-Z0-9-/,. ][a-zA-Z0-9]$".toRegex()
return description.matches(regex)
}
英文:
I need regex that controls "00 XX 000 " pattern
please help
private fun isTransferDescriptionValid(description: String): Boolean {
val regex = "^[a-zA-Z0-9] [a-zA-Z0-9-/,. ][a-zA-Z0-9]$".toRegex()
return description.matches(regex)
}
答案1
得分: 0
你可以使用以下正则表达式,它将匹配 00 XX 000
(\s
用于表示空格):
val regex = "^[0-9]{2}\\s[a-zA-Z]{2}\\s[0-9]{3}$".toRegex()
然后还有 [:space:]
:
val regex = "^[0-9]{2}[[:space:]]{1}[a-zA-Z]{2}[[:space:]]{1}[0-9]{3}$".toRegex()
但有时只需要一个字面上的空格:
val regex = "^[0-9]{2} [a-zA-Z]{2} [0-9]{3}$".toRegex()
英文:
You can use the following regex, which would find 00 XX 000
(\s
is a general placeholder for space)
val regex = "^[0-9]{2}\\s[a-zA-Z]{2}\\s[0-9]{3}$".toRegex()
then there is also [:space:]
val regex = "^[0-9]{2}[[:space:]]{1}[a-zA-Z]{2}[[:space:]]{1}[0-9]{3}$".toRegex()
But sometimes only a literal space helps
val regex = "^[0-9]{2} [a-zA-Z]{2} [0-9]{3}$".toRegex()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论