正则表达式 - 从String确定矩阵 [英] Regex - determine matrix from String
本文介绍了正则表达式 - 从String确定矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为String创建简单矩阵,可以包含以下组合
how can I create simple matrix for String that could be contains following combinations
123456 ABC
123456AB1
123456AB12
123456AB123
123456
例如
if ("\\d + \\d + \\d + \\d + \\d + \\d
+ \\s
+ [a-zA-Z]+[a-zA-Z]+[a-zA-Z]") {
//passed variant from input in form 123456 ABC
} else if ("\\d + \\d + \\d + \\d + \\d + \\d
+ [a-zA-Z]+[a-zA-Z]+[a-zA-Z]
+ \\d") {
//passed variant from input in form 123456AB1
} else if ("\\d + \\d + \\d + \\d + \\d + \\d
+ [a-zA-Z]+[a-zA-Z]+[a-zA-Z]
+ \\d + \\d") {
//passed variant from input in form 123456AB12
} else if ("\\d + \\d + \\d + \\d + \\d + \\d
+ [a-zA-Z]+[a-zA-Z]+[a-zA-Z]
+ \\d + \\d + \\d") {
//passed variant from input in form 123456AB123
} else if ("\\d + \\d + \\d + \\d + \\d + \\d") {
//passed variant from input in form 0123456
} else {
//doesn't match
}
推荐答案
您可以使用以下正则表达式
You could for instance use the following regexs
123456 ABC -> \\d{6}\\s\\w{3}
123456AB1 -> \\d{6}\\w{3}
123456AB12 -> \\d{6}\\w{4}
123456AB123 -> \\d{6}\\w{5}
123456 -> \\d{6}
if-clauses可以在你的例子中使用,例如,
The if-clauses can be used as in your example, e.g.,
if(str.matches("\\d{6}\\s\\w+") {
...
} ...
正如你的问题,这些正则表达式变体仅涵盖此示例中的确切组合。
Just as your question, these regex variants only covers the exact combinations from this example.
这篇关于正则表达式 - 从String确定矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文