Regex Implementation for Beginners

Shivam Sharma
3 min readAug 8, 2019

--

Most of the programmers and developers heard about this Regex(Regular Expression) as all know. Almost in all programming we use Regex.

First question come in my mind is Why we need this Regex?

So in Android Development when we send data to server we validate that data on client side. In this I got my First question’s answer for validate the data, search some specific word from String and match patterns according to requirements.

After first question Second question arise Why we use Regex in our code?

In Android provide some patterns for validate data like Email Id and numbers, but sometime we need to validate data with specific words or character. So on that time we need to implement Regex in our code to search and validate data. This resolve my Second question doubt.

After that i start learning Regex and share some common expression used in Regex with example.

Common used Regex

Character Class:- ^,[abc],[a-z],.,[\w\W],[\s\S],[\d\D]

Character Set [aeiou] :- Match any Character that is provided in Square Bracket set. In this Square Bracket you can add character as your wish no restriction.

Example:- I have following string if i applied character set on this string it will return the value i highlighted in bold values “Your regex post is cool.”

“Your regex post is cool.”

Negated Set [^aeiou]:- Match any character that is not provide in Square Brackets. Except “aeiou” characters it select all other character. I am using same string for Example:- Your regex post is cool.”

Range [a-z]:- Match any character that provide by user From — To range in Square brackets. Like [c-f] and i have string “abcdefghi”. We apply range from regex it give you all character from [c-f] like that “cdef”.

Dot . :- Match any character except new line (\n).

[\s\S] :- \s match any whitespaces character (space, tab spacing, new line). \S match any character that is not whitespace(space, tab spacing, new line).

[\w\W]:- \w match any word or \W match any character that is not word.

[\d\D]:- \d match any digit Character or \D match any character that is not digit.

Anchor :- ^ and $

^Hello :- match any string start with Hello word

word$ :- match any string end with word word

Quantifiers :- *+{}| or ?

ba\w+(Plus):- Match any word start with b and match ‘a’ 1or more.

ba\w* (Star):- match any word or string start with b after that match ‘a’ with zero or more.

ba{2} (Quantifier):- match any word or string start with b after that 2 a’s.

ba{2,} (Quantifier):- match any word or string start with b after that 2 or more a’s in string or word.

ba? (Optional):- match any word or string start with b after that ‘a’ is optional if it is there or not.

b(a|e|i)d (Alternation):- match any word or string start with ‘b’ and contain any character with “a,e,i” and in last ‘d’ is there.

Flags

We all know about regex how we can use it. But regex come with two slash like “/xyz/”. We have following flags:-

g (Global):- this give all the match result in paragraph or string for relevent result. If we are not using /g then it ll give first match result not others.

m(MultiLine):- this flag give multi line result. Like if we want to search or find any word start with “The” and search in every break line for match keyword in multiple lines using “/m”.

i(Insensitive):- regex is case sensitive so if we don’t want use case sensitive on that time we use this flag “/i”.

I learn regex form here :- https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285

Follow this website for more clarification:- https://regexr.com/

I hope this blog is little bit helpful for new programmer or developer for understand regex.

--

--

Shivam Sharma
Shivam Sharma

No responses yet