[英]Telephone number category using regex pattern
I want to get user input for telephone numbers. 我想获得用户输入的电话号码。 I have 2 number categories Golden and Normal.
我有2个数字类别,黄金和普通。 When the user enter certain pattern of a telephone number, the system will automatically determine it as Golden or Normal.
当用户输入特定的电话号码格式时,系统会自动将其确定为“黄金”或“正常”。 I'm having problem to code certain pattern.
我在编码某些模式时遇到问题。 One of the Golden Pattern number is like this: AB001234 where AB is number like 12,23,34,45,56,67,78 and 89. Here what I got so far.
黄金模式编号之一是这样的:AB001234其中AB是编号12,23,34,45,56,67,78和89之类的。到目前为止,我得到了什么。
public static void main(String[] args) {
Scanner userinput = new Scanner(System.in);
System.out.println("Enter Telephone Number");
String nophone = userinput.next();
String Golden = "(\\d)(\\1)002345|(\\d*)12345$";
//I want to add AB001234 pattern to the line above but I don't know how.
if (nophone.matches(Golden)) {
System.out.println("Golden");
}
else {
System.out.println("Normal");
}
}
I'm not sure do I really have to use regex or not. 我不确定我是否真的必须使用正则表达式。 One more question, you can see the first part of String Golden is without $ while the second part has $.
还有一个问题,您可以看到String Golden的第一部分没有$,而第二部分则有$。 I'm not sure the effect if I put or remove the $ symbol.
我不确定放置或删除$符号的效果。
(\\\\d)(\\\\1)
does not check for the sequence like 12
, 23
and so on.. Rather it checks for two smae consecutive digits like 11
, 22
, 33
, ... (\\\\d)(\\\\1)
不检查该序列像12
, 23
等..相反,它会检查像两个SMAE连续的数字11
, 22
, 33
,...
To check for sequence, you would have to do it explicitly using Pipe(|)
- (12|23|34|45|...)
要检查顺序,您必须使用
Pipe(|)
- (12|23|34|45|...)
显式地进行操作
So, your pattern for Golden Number
should be like this: - 因此,您的
Golden Number
格式应如下所示:-
^(?:12|23|34|45|56|67|78|89)001234$
(?:..)
- Means a non-capturing group
. (?:..)
-表示non-capturing group
。 It will not be captured as a numbered group in your pattern. 在您的图案中,它不会被捕获为带编号的组。
NOTE: - If the length of your sequence
is varying, then Regex
is not an appropriate way to match them. 注意:-如果
sequence
的长度不同,则Regex
不是匹配它们的适当方法。
For your second question, $
denotes the end of the string. 对于第二个问题,
$
表示字符串的结尾。 So, the pattern with $
at the end, will be matched at the end of the string. 因此,以
$
结尾的模式将在字符串的末尾匹配。 Also, Caret (^)
is there to match the beginning of the string. 另外,
Caret (^)
可以匹配字符串的开头。
For eg: - 例如:-
abc$
matches the string "asdfabc"
, but not "sdfabcf"
. abc$
与字符串"asdfabc"
匹配,但与"asdfabc"
不"sdfabcf"
。 ^abc
matches the string "abcfsdf"
, but not "sdfabcf"
. ^abc
与字符串"abcfsdf"
匹配,但与"abcfsdf"
不"sdfabcf"
。 ^abc$
only matches the string "abc"
, because it is the only string, that starts and ends with "abc"
. ^abc$
只匹配字符串"abc"
,因为它是唯一以"abc"
开头和结尾的字符串。 You can go through the following links to learn more about Regexp
: - 您可以通过以下链接来了解有关
Regexp
更多信息:-
To get this: 为了得到这个:
AB001234 where AB is number like 12,23,34,45,56,67,78 and 89. Here what I got so far
AB001234,其中AB是12、23、34、45、56、67、78和89之类的数字。
The regex would look like: 正则表达式如下所示:
^(12|23|34|45|56|67|78|89)001234$
The $
symbol means end of the string. $
符号表示字符串的结尾。 This means that if there is any aditional character after the last one, the string won't match the Regex. 这意味着,如果在最后一个字符之后有任何其他字符,则该字符串将与正则表达式不匹配。
The ^
symbol means the begining of the string. ^
符号表示字符串的开头。
For further information, please, check the Summary of regular-expression constructs at the Javadoc API. 有关更多信息,请在Javadoc API上查看正则表达式构造的摘要 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.