Yugesh
Yugesh

Reputation: 4092

How to create regex pattern for validating editext in android?

am create reg-ex for validating edit text field.am not able to validate correctly.

my condition to validate is 
Example (h2/g2)
At-least have a one numerical value
slash also allowed but only one,after the "/" must have one numerical value.
alphabets also allowed

How to create reg-ex for this condition.Can any one know please help me to solve this problem

Upvotes: 2

Views: 1325

Answers (1)

MaciejGórski
MaciejGórski

Reputation: 22232

Seems like you want something like this: (\w*/)?\w*\d\w*

Remember that in Java you have to duplicate backslashes \\.

Adding more examples of valid and invalid texts would help.

Edit:

Answering you comment. Yes, it does:

String value = "h2/g2";
Pattern p = Pattern.compile("(\\w*/)?\\w*\\d\\w*");
Matcher m = p.matcher(value);
Log.i("tag", "matches? " + m.matches());

prints:

I/tag(12642): matches? true

Upvotes: 2

Related Questions