Regex basics

Regex or regular expression is a very powerful tool. This tutorial is a very basic tutorial on regex.
Before starting, I like to give some info,

(^) carrot symbol represents start of string and 
($) dollar symbol represents end of string

Difference between '*' and '+' in regex

+ means one or more of the previous atom. {1,}
* means zero or more. this can match nothing in addition to the characters specified in your square bracket expression {0,}

Difference between '*' and '.*' in regex

. means any character. * means previous character (or group) 0 or more times. So .* means any character 0 or more times. That's it
* is a quantifier that modifies the meaning of the previous character, group or character class in the regex. On its own it is meaningless. It only carries meaning when combined with what is immediately before it in the regex.

For optional cases, in regex, we use "?" symbol. eg. in email address, special character _ can be used. To check that we can use _?

\s for alphabets and
\d for numbers

To check the length, we use {} eg: {6,} means regex match all string which has at least 6 characters

Now, I am going to explain some basic methods to check whether the string has only alphabets, only numbers, last 4 characters are numbers etc.

You can check the source code here


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android