File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .regex .Matcher ;
2+ import java .util .regex .Pattern ;
3+
4+ /**
5+ * Example of using regular expressions for finding strings.
6+ */
7+ public final class Match {
8+
9+ /**
10+ * Private constructor prevents class from being instantiated.
11+ */
12+ private Match () {
13+
14+ }
15+
16+ /**
17+ * Entry point of the program.
18+ *
19+ * @param args command-line arguments, not used
20+ */
21+ public static void main (final String [] args ) {
22+ String input1 = "My email is someones@mail.com" ;
23+ String input2 = "My email is not@mail" ;
24+ String regularExpression = "[^\\ s]+@[^\\ s]+\\ .[^\\ s]+" ;
25+
26+ Pattern pattern = Pattern .compile (regularExpression );
27+
28+ Matcher matcher1 = pattern .matcher (input1 );
29+ String output1 = "Nothing was found" ;
30+ if (matcher1 .find ()) {
31+ output1 = matcher1 .group ();
32+ }
33+ System .out .println (output1 );
34+
35+ Matcher matcher2 = pattern .matcher (input2 );
36+ String output2 = "Nothing was found" ;
37+ if (matcher2 .find ()) {
38+ output2 = matcher2 .group ();
39+ }
40+ System .out .println (output2 );
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ import java .util .regex .Matcher ;
2+ import java .util .regex .Pattern ;
3+
4+ /**
5+ * Example of using regular expressions for replacing strings.
6+ */
7+ public final class Replace {
8+
9+ /**
10+ * Private constructor prevents class from being instantiated.
11+ */
12+ private Replace () {
13+
14+ }
15+
16+ /**
17+ * Entry point of the program.
18+ *
19+ * @param args command-line arguments, not used
20+ */
21+ public static void main (final String [] args ) {
22+ //Using String API
23+ String regularExpression1 = "abc" ;
24+ String input1 = "abcdefg" ;
25+ String output1 = input1 .replaceAll (regularExpression1 , "123" );
26+ System .out .println (output1 );
27+
28+ // Using regex API
29+ String regularExpression2 = "abc" ;
30+ String input2 = "abcdefgABCDEFG" ;
31+ Pattern pattern = Pattern .compile (regularExpression2 ,
32+ Pattern .CASE_INSENSITIVE );
33+ Matcher matcher = pattern .matcher (input2 );
34+ String output2 = matcher .replaceAll ("789" );
35+ System .out .println (output2 );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .regex .Pattern ;
3+
4+ /**
5+ * Example of using regular expressions for splitting strings.
6+ */
7+ public final class Split {
8+
9+ /**
10+ * Private constructor prevents class from being instantiated.
11+ */
12+ private Split () {
13+
14+ }
15+
16+ /**
17+ * Entry point of the program.
18+ *
19+ * @param args command-line arguments, not used
20+ */
21+ public static void main (final String [] args ) {
22+ String input = "Hello World, here we are!" ;
23+
24+ //Using String API
25+ String [] result1 = input .toLowerCase ().split ("\\ W+" );
26+ System .out .println (Arrays .toString (result1 ));
27+
28+ // Using regex API
29+ Pattern pattern = Pattern .compile ("\\ W+" );
30+ String [] result2 = pattern .split (input .toLowerCase ());
31+ System .out .println (Arrays .toString (result2 ));
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments