In this tutorial series post,
- I will give a brief introduction to the regular expression.
- will cover character class, repetitions, grouping and capturing, back-references, assertions
- Then solve some examples
- Then practice some hackerrank questions
- Testing the knowledge through the quiz with our handpicked questions
Regular Expression(RegEx):
A regular expression is a sequence of characters that define a search pattern. It is mainly used for string pattern matching.
Before going to the explanation, I used Java language to solve the coding problems.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Regex_Pattern { public static void main(String[] args) { Regex_Test tester = new Regex_Test(); tester.checker("__________"); //here you will paste the regEx pattern } } class Regex_Test { public void checker(String Regex_Pattern){ Scanner Input = new Scanner(System.in); String Test_String = Input.nextLine(); Pattern p = Pattern.compile(Regex_Pattern); Matcher m = p.matcher(Test_String); int Count = 0; while(m.find()){ Count += 1; } System.out.format("Number of matches : %d",Count); } }
Matching Specific String
Pattern : blogger
Test_String: www.blogger.com
Matching Anything
. dot
dot (.) matches anything except a newline or line break
Pattern : d.oye.l
Test_String: dooyell
Matching Digits & Non-Digits Characters
\d
\d matches any digit from 0-9
\D
\D matches any characters that are not digits means excluding 0-9
Pattern : doyel\d\d
Test_String: doyel99
Pattern : \D\d\d
Test_String: d10
Matching Whitespace & Non-Whitespace Character
\s
\s matches any whitespace characters(spaces, tabs, line breaks)[\r\t\n\f]
\S
\S matches any non-whitespace characters
Pattern : \d\s\D
Test_String: 9 u
Matching Word & Non-Word Character
\w
\w matches any word characters alphanumeric and underscore [a-zA-z0-9_]
\W
\W matches any character that is not word character [^a-zA-Z0-9_]
Pattern : \w\W\w
Test_String: o@p
Matching Start & End
^
The ^ symbol matches the beginning of the string or beginning of a line if multiline flag(m) is enabled.
$
The $ symbol matches the end of the string or end of a line if the multiline flag(m) is enabled.
Pattern : ^blogger$
Test_String: blogger
Test_String: www.blogger.com( here no match)
Matching Specific Characters
[]
matches only one out of several characters placed inside the set [].This is also known as character class.
Pattern : [13579] is an odd digit
Test_String: 3 is an odd digit
Excluding Specific Characters
[^]
The negated character class [^] matches any character that is not in the square brackets.
The negated character class [^] matches any character that is not in the square brackets.
Pattern : [^13579] is an even digit
Test_String: 4 is an even digit
Matching Character Ranges
A hyphen (-) inside a character class specifies a range of characters where the left and right operands are the respective lower and upper bounds of the range.
[a-zA-Z0-9_] this is the character range of \w
The negated character class [^] matches any character that is not in the square brackets.
The negated character class [^] matches any character that is not in the square brackets.
[^a-zA-Z0-9_] this is the character range of \W
Pattern : [x-z][7-9]
Test_String: y8
Note: Use \\ instead of using \ in java
Post a Comment
Post a Comment
You are welcome to share your thoughts with us!