File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44/**
55 * Example of using regular expressions for replacing strings.
66 */
7- public final class Replace {
8-
7+ final class Replace {
98 /**
109 * Private constructor prevents class from being instantiated.
1110 */
Original file line number Diff line number Diff line change 44/**
55 * Example of using regular expressions for splitting strings.
66 */
7- public final class Split {
8-
7+ final class Split {
98 /**
109 * Private constructor prevents class from being instantiated.
1110 */
@@ -21,7 +20,7 @@ private Split() {
2120 public static void main (final String [] args ) {
2221 String input = "Hello World, here we are!" ;
2322
24- //Using String API
23+ // Using String API
2524 String [] result1 = input .toLowerCase ().split ("\\ W+" );
2625 System .out .println (Arrays .toString (result1 ));
2726
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+ final class Match {
8+ /**
9+ * Private constructor prevents class from being instantiated.
10+ */
11+ private Match () {
12+
13+ }
14+
15+ /**
16+ * Entry point of the program.
17+ *
18+ * @param args command-line arguments, not used
19+ */
20+ public static void main (final String [] args ) {
21+ String [] inputs = {"My email is someones@mail.com" ,
22+ "My email is not@mail" };
23+ String regularExpression = "[^\\ s]+@[^\\ s]+\\ .[^\\ s]+" ;
24+
25+ Pattern pattern = Pattern .compile (regularExpression );
26+
27+ for (String input : inputs ) {
28+ Matcher matcher = pattern .matcher (input );
29+ String output = "nothing was found" ;
30+ if (matcher .find ()) {
31+ output = matcher .group ();
32+ }
33+ System .out .println (String .format ("'%s': %s" , input , output ));
34+ }
35+ }
36+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments