Regular expression (aka regex) is a powerful tool for matching and verifying strings. Without them, there would be a lot of trouble making programs verify that the text inputted in a field is valid.
In this post, I will be going over how to do regex with an Android app. We will have an EditText that the user will input the text, a Button that the user will press to test if the String they wrote is a match for the regex, and a TextView that displays either "true" or "false" depending if its a match or not.
In Android (or java), you can test if a string matches a regular expression by calling:
In this post, I will be going over how to do regex with an Android app. We will have an EditText that the user will input the text, a Button that the user will press to test if the String they wrote is a match for the regex, and a TextView that displays either "true" or "false" depending if its a match or not.
In Android (or java), you can test if a string matches a regular expression by calling:
String.matches(String regularExpression);
This returns a boolean of either true or false, depending on the match. Lets try this out with a question:
Verify that the text inputted is a valid phone number.
One way we can do this is by doing the following:
Verify that the text inputted is a valid phone number.
One way we can do this is by doing the following:
inputString.matches("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]");
As you can see, this is a very basic (and ineffective) way to do regular expressions. For one, it repeats the entries needlessly. And two, it doesn't account for parentheses, or a phone number without dashes. A better way to do this is with the "?" qualifier, which makes it so that it will pass with or without the character before it. Here is an example:
inputString.matches("[0-9]{3}-?[0-9]{3}-?[0-9]{4}");
Here you can see that we have incorporated grouping with the {n} expression. This means that it will match n amount of times for the character that comes before it. Again, this can be improved by using shorthand, and also by accounting for parentheses.
inputString.matches("\\(?\\d{3}[)-]?\\d{3}-?\\d{4}");
The "\\d" is shorthand for any digits that are 0-9. "\\" tells the expression to "escape" the next character, meaning that it will be come a literal character and be part of the string. Here we tell it to escape the open parentheses "(" and then add a "?" after it, which makes it optional. We then form a group with the "[)-]" expression, stating that either one of them could be optional, but not both at the same time.
As you can see here, regular expressions can be a bit...difficult to read at times. It is usually preferred to comment what the format is that you are looking for in your code, so that co-workers will not have to sort through and figure out what you are matching for.'
Luckily, Android comes with a utility class called Patterns, which is sort of a high-level regex matching tool. It comes with pre-made regex patterns for easy matching. For example if you want to see if a string is a valid email address, you can simply put:
As you can see here, regular expressions can be a bit...difficult to read at times. It is usually preferred to comment what the format is that you are looking for in your code, so that co-workers will not have to sort through and figure out what you are matching for.'
Luckily, Android comes with a utility class called Patterns, which is sort of a high-level regex matching tool. It comes with pre-made regex patterns for easy matching. For example if you want to see if a string is a valid email address, you can simply put:
Patterns.EMAIL_ADDRESS.matcher(mEditText.getText().toString());
Simple right?
The Patterns class is usually good enough to suit developers' needs when it comes to common regular expressions. But even when its not, we now know how to do regex the old-fashioned way!
I have uploaded a sample project that lets users play around with regex on github:
https://github.com/allenktv/AndroidDemos/tree/master/Regex
The Patterns class is usually good enough to suit developers' needs when it comes to common regular expressions. But even when its not, we now know how to do regex the old-fashioned way!
I have uploaded a sample project that lets users play around with regex on github:
https://github.com/allenktv/AndroidDemos/tree/master/Regex