开发者

Help in writing REGEX

I have to take user input from a HTML form in this format (field1)(space)(field2) where both field1 and field2 are Strings but the restrictions are:

  • field1 & field2 can't be integers
  • they must be alphanumeric
  • they should not start with the same c开发者_如何学编程haracter

Note that this pattern can repeat itself, eg:

abc def ghi jkl


field1 & field2 can't be integers

(?!\p{Digit}+\b)

they must be alphanumeric

and presumably non-empty

\p{Alnum}+

they should not start with the same character

requires capturing the first character in a capturing group so the above becomes

(\p{Alnum})\p{Alnum}*

and you need a negative lookahead

(?!\1)

Putting it all together and allowing separating you can use the following

Pattern.compile("^\\s*(?:(?!\\p{Digit}+\\b)(?!\1)(\\p{Alnum})\\p{Alnum}*\\s*)+\\Z");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜