Blog

What is RegEx ( Regular Expression ) :

Regular Expression are specially encoded text strings used as Patterns for matching set of Strings.
Regular Expression is also known as RegEx.

RegEx is very powerful tool in the toolkit of programmers who are dealing with Text Processing or Text Mining.

Thus , Regexes are an extremely flexible tool for finding and replacing text. They can easily be applied globally across a document, dataset, or specifically to individual strings.

Now lets decipher Regex :

A) Components of Regular Expression -

>> Characters 

>> Meta Characters 

>> Character Classes 

B) Meta-character Symbols

^ anchors expression to beginning of target
$ anchors expression to end of target
. matches any single character except newline
| separates alternative patterns
[] accepts any of the enclosed characters
[^] accepts any characters but the ones enclosed in brackets
() groups patterns together for assignment or constraint
* matches zero or more occurrences of preceding entity
? matches zero or one occurrences of preceding entity
+ matches one or more occurrences of preceding entity
{n} matches exactly n occurrences of preceding entity
{n,} matches at least n occurrences of preceding entity
{n,m} matches n to m occurrences of preceding entity


Examples of the above - 

Example 1 : matches one or more alphabetic characters
> str_detect(c("Abcd","12ab","1234"),"[a-zA-Z]+")
[1]  TRUE  TRUE FALSE


Example 2 : matches zero or more of any character
> str_detect(c("Abcd","12ab",""),".*")
[1] TRUE TRUE TRUE

Example 3 : matches zero or more characters followed by a literal ".*"
> str_detect(c("Ab.*",".*12","*"),".*.*")
[1]  TRUE  TRUE FALSE

Example 4 : Match “abc” or “123”
> str_detect(c("Xabc1","a123z","a1b2c3"),"(abc|123)")
[1]  TRUE  TRUE FALSE

Example 5 : Match a, b, c, 1, 2 or 3.
> str_detect(c("Xa","yb","Vc","w1","P2","z4"),"[abc123]")
[1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE

Example 6 : Matches lines starting with “From:” or “Subject:” or “Date:”
> str_detect(c("From:XYZ","Date:2018","BCC:") , "^(From|Subject|Date):")
[1]  TRUE  TRUE FALSE
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment