用户输入任何字符串,程序会区分字符串是否符合条件的产品ID.
符合条件的产品ID是由两个大写字母和四个数字组成的字符串中的任何一个.(例如,"TV1523")
我该如何制作这个节目?
Ewa*_*ald 35
您应该使用正则表达式比较字符串,例如:
str.matches("^[A-Z]{2}\\d{4}") 会给你一个关于它是否匹配的布尔值.
正则表达式的工作方式如下:
^ Indicates that the following pattern needs to appear at the beginning of the string.
[A-Z] Indicates that the uppercase letters A-Z are required.
{2} Indicates that the preceding pattern is repeated twice (two A-Z characters).
\\d Indicates you expect a digit (0-9)
{4} Indicates the the preceding pattern is expected four times (4 digits).
Run Code Online (Sandbox Code Playgroud)
使用此方法,您可以遍历任意数量的字符串并检查它们是否与给定的条件匹配.
您应该阅读正则表达式,如果您担心性能,可以使用更有效的方式存储模式.
你应该仔细看看正则表达式.教程例如在regular-expressions.info上.
你的模式的一个例子可能是
^[A-Z]{2}\d{4}$
Run Code Online (Sandbox Code Playgroud)
你可以在Regexr.com上看到它是一个在线测试正则表达式的好地方.
这里是java正则表达式教程,你可以看到你如何用Java调用它们.
| 归档时间: |
|
| 查看次数: |
53476 次 |
| 最近记录: |