Regex to match numeric and special chatacters

时间:2017-04-05 08:25:26

标签: java

I need regex which matches as per below values:

  1. abc123 - Do not match
  2. 123 - Match
  3. a123bc - Do not match
  4. a#b%c - Match
  5. 13&ab12% - Match
  6. আফ্রিকার - Do not match

In short, it matches with only numeric string and any string with special character(#,&,%). Also it do not match with any other language string.

I tried this regex but it didn't work for me.

"^[\\d][#&%]+$"

2 个答案:

答案 0 :(得分:0)

尝试类似

的内容
"^\\d+|(.*[#&%].*)$"

答案 1 :(得分:0)

尝试以下正则表达式:

^((?=.*[#&%]).+|\d+)$

说明:

^               -> Start of the string
(?=.*[#&%]).+   -> Match at least one special character (#,&,%) from One or more character
|               -> Or Condition
\d              -> Match digit
$               -> End of the String