Mdb
Mdb

Reputation: 8568

RegEx for validating specific input

The input accepts data in format: %'letter' separator %'letter' and so on.

The separator can be: space, tab, colon or new line

Example:

%d %F %S   
or     
%d:%F:%S    
or    
%d:%F
%S

where the character after the '%' can be any letter

You cannot write something like:

%dddd %F %S
or 
%%d %F %S

So far I have done this:

^((%([a-zA-Z]){1})+\s?|:)+$

Any help will be appreciated.

Upvotes: 0

Views: 50

Answers (2)

rzymek
rzymek

Reputation: 9281

You can go with this

(%[a-zA-Z][ \t\n;])*%[a-zA-Z]

Regular expression visualization

But I'd suggest passing an case-insensitive flag to your matcher. Then the expression can be shortened to

(%[a-z][ \t\n;])*%[a-z]

Upvotes: 1

Nicola Miotto
Nicola Miotto

Reputation: 3696

How about

^((%[A-z])(\s|:))*(%[A-z])$

http://regexr.com?37626

Upvotes: 2

Related Questions