Aditya Vikas Devarapalli
Aditya Vikas Devarapalli

Reputation: 3473

Regex - text Matching

I need a regex that will match string S with the following conditions:

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.

I wrote the following, but it is not working.

^([a-z])(\w)(\s)(\W)(\d)([A-Z])([a-zA-Z])([a,e,i,o,u,A,E,I,O,U])(\S)(\D)\1\2\3\4\5\6\7\8\9\10$

Sample Input:

ab #1?AZa$ab #1?AZa$

Can someone explain me what's wrong with this ?

Thank you.

Upvotes: 2

Views: 147

Answers (2)

Saleem
Saleem

Reputation: 8978

You may want to try this way:

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

to capture individual class/group of character. However, if you don't need to capture individual character you can consider solution posted above.

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use this regex:

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

RegEx Demo

  1. As per question 6th position is non-digit but your reges has [A-Z].
  2. There is no need to have commas inside a character class for multiple values.
  3. There is no need to capture 10 values separately for 10 back-references as char 1-10 are same as char 11-20. We can just capture first 10 characters into one group and use it as a single back-reference in the end.

Upvotes: 3

Related Questions