2014-01-12 85 views
1

我正在使用java编程语言。 我想写一个正则表达式,该字符串匹配specific string(e.g. boy)或字符串开头的数字(from 5-8)。 成功输入只能是:正则表达式匹配特定的字符串或数字

  1. 男孩....
  2. 7 ....(7这里可以用5,6,8代替)

如何做到这一点?

在所有其他情况下,返回值应为false.Any解决方案或建议将不胜感激。

回答

2

你可以使用这个表达式:

^(boy|[5-8]) 

说明:

^ assert position at start of the string 
1st Capturing group (boy|[5-8]) 
1st Alternative: boy 
boy matches the characters boy literally (case sensitive) 
2nd Alternative: [5-8] 
[5-8] match a single character present in the list below 
5-8 a single character in the range between 5 and 8 
+0

非常感谢,这是非常有用的 – user3162489

+0

不客气,很高兴它的工作。 – anubhava