Skip to content

Commit 6ae79ba

Browse files
committed
added regex
1 parent 77f6019 commit 6ae79ba

File tree

3 files changed

+212
-2
lines changed

3 files changed

+212
-2
lines changed

.idea/workspace.xml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ It is used to develop rich internet applications. It uses a light-weight user in
118118
|[Arrays in Java](https://github.com/connectaman/Java_Notes_and_Programs/tree/master/src/Arrays)|
119119
|[]()|
120120
|[]()|
121+
|[]()|
122+
|[]()|
123+
|[]()|
124+
|[]()|
125+
|[Java Regex]()|
126+
|[]()|
127+
|[]()|
128+
|[]()|
129+
|[]()|
130+
|[]()|
131+
|[]()|
132+
|[]()|
133+
|[]()|
134+
|[]()|
135+
121136

122137

123138

src/Regex/Regex.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
### Java Regex
2+
3+
The Java Regex or Regular Expression is an API to define a pattern for searching or manipulating strings.
4+
5+
It is widely used to define the constraint on strings such as password and email validation. After learning Java regex tutorial, you will be able to test your regular expressions by the Java Regex Tester Tool.
6+
7+
Java Regex API provides 1 interface and 3 classes in java.util.regex package.
8+
9+
##### java.util.regex package
10+
11+
The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.
12+
13+
1- MatchResult interface
14+
15+
2- Matcher class
16+
17+
3- Pattern class
18+
19+
4- PatternSyntaxException class
20+
21+
![](https://static.javatpoint.com/images/java-regex-api.jpg)
22+
23+
24+
25+
------
26+
27+
##### Matcher class
28+
29+
It implements the MatchResult interface. It is a regex engine which is used to perform match operations on a character sequence.
30+
31+
|No.| Method| Description|
32+
|-----|-----|--------|
33+
|1| boolean matches()| test whether the regular expression matches the pattern.|
34+
|2| boolean find()| finds the next expression that matches the pattern.|
35+
|3| boolean find(int start)| finds the next expression that matches the pattern from the given start number.|
36+
|4| String group() |returns the matched subsequence.|
37+
|5| int start() |returns the starting index of the matched subsequence.|
38+
|6| int end() |returns the ending index of the matched subsequence.|
39+
|7| int groupCount() |returns the total number of the matched subsequence.|
40+
41+
-------
42+
43+
##### Pattern class
44+
45+
It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.
46+
47+
|No.| Method| Description|
48+
|-----|------|-------|
49+
|1 |static Pattern compile(String regex) |compiles the given regex and returns the instance of the Pattern.|
50+
|2 |Matcher matcher(CharSequence input)| creates a matcher that matches the given input with the pattern.|
51+
|3 |static boolean matches(String regex, CharSequence input)| It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.|
52+
|4 |String[] split(CharSequence input) |splits the given input string around matches of given pattern.|
53+
|5 |String pattern() |returns the regex pattern.|
54+
55+
------
56+
57+
##### Regular Expression . Example
58+
59+
```java
60+
import java.util.regex.*;
61+
class RegexExample2{
62+
public static void main(String args[]){
63+
System.out.println(Pattern.matches(".s", "as"));//true (2nd char is s)
64+
System.out.println(Pattern.matches(".s", "mk"));//false (2nd char is not s)
65+
System.out.println(Pattern.matches(".s", "mst"));//false (has more than 2 char)
66+
System.out.println(Pattern.matches(".s", "amms"));//false (has more than 2 char)
67+
System.out.println(Pattern.matches("..s", "mas"));//true (3rd char is s)
68+
}
69+
}
70+
```
71+
72+
##### Regex Character classes
73+
74+
|No. |Character Class| Description|
75+
|-----|-------|---------|
76+
|1| [abc] |a, b, or c (simple class)|
77+
|2| [^abc] |Any character except a, b, or c (negation)|
78+
|3| [a-zA-Z]| a through z or A through Z, inclusive (range)|
79+
|4| [a-d[m-p]] |a through d, or m through p: [a-dm-p] (union)|
80+
|5| [a-z&&[def]]| d, e, or f (intersection)|
81+
|6| [a-z&&[^bc]]| a through z, except for b and c: [ad-z] (subtraction)|
82+
|7| [a-z&&[^m-p]]| a through z, and not m through p: [a-lq-z](subtraction)|
83+
84+
85+
86+
---------
87+
88+
##### Regular Expression Character classes Example
89+
90+
```java
91+
import java.util.regex.*;
92+
class RegexExample3{
93+
public static void main(String args[]){
94+
System.out.println(Pattern.matches("[amn]", "abcd"));//false (not a or m or n)
95+
System.out.println(Pattern.matches("[amn]", "a"));//true (among a or m or n)
96+
System.out.println(Pattern.matches("[amn]", "ammmna"));//false (m and a comes more than once)
97+
}
98+
}
99+
```
100+
101+
##### Regex Quantifiers
102+
103+
The quantifiers specify the number of occurrences of a character.
104+
105+
|Regex |Description|
106+
|---------|---------|
107+
|X? |X occurs once or not at all|
108+
|X+ |X occurs once or more times|
109+
|X* |X occurs zero or more times|
110+
|X{n} |X occurs n times only|
111+
|X{n,}| X occurs n or more times|
112+
|X{y,z} |X occurs at least y times but less than z times|
113+
114+
115+
---------
116+
117+
##### Example
118+
```java
119+
import java.util.regex.*;
120+
class RegexExample4{
121+
public static void main(String args[]){
122+
System.out.println("? quantifier ....");
123+
System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time)
124+
System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time)
125+
System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (a m and n comes more than one time)
126+
System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a comes more than one time)
127+
System.out.println(Pattern.matches("[amn]?", "am"));//false (a or m or n must come one time)
128+
129+
System.out.println("+ quantifier ....");
130+
System.out.println(Pattern.matches("[amn]+", "a"));//true (a or m or n once or more times)
131+
System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a comes more than one time)
132+
System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a or m or n comes more than once)
133+
System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z and t are not matching pattern)
134+
135+
System.out.println("* quantifier ....");
136+
System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (a or m or n may come zero or more times)
137+
138+
}
139+
}
140+
```
141+
142+
----------
143+
144+
##### Regex Metacharacters
145+
146+
The regular expression metacharacters work as shortcodes.
147+
148+
|Regex |Description|
149+
|------|--------|
150+
|. |Any character (may or may not match terminator)|
151+
|\d |Any digits, short of [0-9]|
152+
|\D |Any non-digit, short for [^0-9]|
153+
|\s |Any whitespace character, short for [\t\n\x0B\f\r]|
154+
|\S |Any non-whitespace character, short for [^\s]|
155+
|\w |Any word character, short for [a-zA-Z_0-9]|
156+
|\W |Any non-word character, short for [^\w]|
157+
|\b |A word boundary|
158+
|\B| A non word boundary|
159+
160+
161+
---------
162+
163+
##### Regular Expression Metacharacters Example
164+
165+
```java
166+
import java.util.regex.*;
167+
class RegexExample5{
168+
public static void main(String args[]){
169+
System.out.println("metacharacters d....");\\d means digit
170+
171+
System.out.println(Pattern.matches("\\d", "abc"));//false (non-digit)
172+
System.out.println(Pattern.matches("\\d", "1"));//true (digit and comes once)
173+
System.out.println(Pattern.matches("\\d", "4443"));//false (digit but comes more than once)
174+
System.out.println(Pattern.matches("\\d", "323abc"));//false (digit and char)
175+
176+
System.out.println("metacharacters D....");\\D means non-digit
177+
178+
System.out.println(Pattern.matches("\\D", "abc"));//false (non-digit but comes more than once)
179+
System.out.println(Pattern.matches("\\D", "1"));//false (digit)
180+
System.out.println(Pattern.matches("\\D", "4443"));//false (digit)
181+
System.out.println(Pattern.matches("\\D", "323abc"));//false (digit and char)
182+
System.out.println(Pattern.matches("\\D", "m"));//true (non-digit and comes once)
183+
184+
System.out.println("metacharacters D with quantifier....");
185+
System.out.println(Pattern.matches("\\D*", "mak"));//true (non-digit and may come 0 or more times)
186+
187+
}
188+
}
189+
```
190+
191+
192+
193+

0 commit comments

Comments
 (0)