使用正则表达式来判断输入是否为11位数字。以下是一个示例代码:\n\nscala\nimport scala.util.matching.Regex\n\ndef is11Digits(input: String): Boolean = {\n val pattern: Regex = \"\"\"^\d{11}$\"\"\".r\n pattern.matches(input)\n}\n\nval input1 = \"12345678901\"\nval input2 = \"123456789012\"\n\nprintln(is11Digits(input1)) // 输出: true\nprintln(is11Digits(input2)) // 输出: false\n\n\n在上述代码中,我们使用了正则表达式 \"\"\"^\d{11}$\"\"\" 来匹配11位数字。其中, ^ 表示字符串的开始, \d 表示数字, {11} 表示前面的元素重复11次, $ 表示字符串的结束。r 方法将字符串转换为 Regex 对象,然后使用 matches 方法来判断输入是否匹配该正则表达式。


原文地址: https://www.cveoy.top/t/topic/pwLL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录