Regex用于控制输入模板:2个数字-空格-2个字母-空格-3个数字。

huangapple go评论53阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月12日 22:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定