regex - Android regular expression -


i have searched lot regular expression or pattern work me, haven't found any.

in edit text want allow first 4 digit , 2 uppercase letters, ihave created pattern:

private final pattern spattern  = pattern.compile("^[0-9]{0,4}[a-z]{0,2}"); 

but allows first 2 capital letters, too.

if change pattern

private final pattern spattern  = pattern.compile("^[0-9]{0,4}[a-z]{4,6}");  

i not able rright.

please me this.

thanks.

you need careful limiting quantifier. remove 0, allows less 4 digits or 2 uppercase letters:

^[0-9]{4}[a-z]{2} 

this require 4 digits @ beginning , 2 letters after.

see demo

for live validation, can use

^[0-9]{0,4}(?:(?<=[0-9]{4})[a-z]{0,2})? 

it allow user input 0 4 digits , 0 2 english uppercase letters only if there 4 digits before them. mind if input can contain these max 6 characters, can add $ end-of-string anchor @ end.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -