正则表达式 - 文本匹配

时间:2016-01-08 21:44:20

标签: regex string

我需要一个将字符串S与以下条件匹配的正则表达式:

server_name

我写了以下内容,但它无效。

List<Fee> unpaidFees = new ArrayList<>(allfees);
unpaidFees.removeAll(paidfees);

示例输入:

S must be of length: 20
1st character: lowercase letter.
2nd character: word character.
3rd character: whitespace character.
4th character: non word character.
5th character: digit.
6th character: non digit.
7th character: uppercase letter.
8th character: letter (either lowercase or uppercase).
9th character: vowel (a, e, i , o , u, A, E, I, O or U).
10th character: non whitespace character.
11th character: should be same as 1st character.
12th character: should be same as 2nd character.
13th character: should be same as 3rd character.
14th character: should be same as 4th character.
15th character: should be same as 5th character.
16th character: should be same as 6th character.
17th character: should be same as 7th character.
18th character: should be same as 8th character.
19th character: should be same as 9th character.
20th character: should be same as 10th character.

有人可以解释一下我的错误吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

您可以使用此正则表达式:

^([a-z]\w\s\W\d\D[A-Z][a-zA-Z][aeiouAEIOU]\S)\1$

RegEx Demo

  1. 根据问题,第6个位置是非数字位置,但您的位置为[A-Z]
  2. 对于多个值,不需要在字符类中使用逗号。
  3. 不需要为10个反向引用分别捕获10个值,因为char 1-10与char 11-20相同。我们可以将前10个字符捕获到一个组中,并最终将其用作单个反向引用。

答案 1 :(得分:0)

您可能想尝试这种方式:

^([a-z])(\w)(\s)(\W)(\d)(\D)([A-Z])([a-zA-Z])([aeiouAEIOU])(\S)\1\2\3\4\5\6\7\8\9\10$

捕获单个类/字符组。但是,如果您不需要捕获个性,您可以考虑上面发布的解决方案。