1import java.util.regex.Pattern;
2import java.util.regex.Matcher;
3
4public class MatcherExample {
5
6 public static void main(String[] args) {
7
8 String text =
9 "This is the text to be searched " +
10 "for occurrences of the http:// pattern.";
11
12 String patternString = ".*http://.*";
13
14 Pattern pattern = Pattern.compile(patternString);
15
16 Matcher matcher = pattern.matcher(text);
17 boolean matches = matcher.matches();
18
19 int count = 0;
20 while(matcher.find()) {
21 count++;
22 System.out.println("found: " + count + " : "
23 + matcher.start() + " - " + matcher.end());
24 }
25 }
26}
1import java.util.regex.*;
2public class RegexExample1{
3public static void main(String args[]){
4//1st way
5Pattern p = Pattern.compile(".s");//. represents single character
6Matcher m = p.matcher("as");
7boolean b = m.matches();
8
9//2nd way
10boolean b2=Pattern.compile(".s").matcher("as").matches();
11
12//3rd way
13boolean b3 = Pattern.matches(".s", "as");
14
15System.out.println(b+" "+b2+" "+b3);
16}}
1//Ther are three type of REGULAR EXPRESSION
2 //first
3 ^(.+)@(.+)$ "^(.+)@(.+)$\""
4 //second
5 ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$
6 //third
7 ^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?!-)(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$
1import java.io.*;
2import java.util.regex.*;
3
4public class testRegex {
5
6 private static Pattern pattern;
7 private static Matcher matcher;
8
9 public static void main(String args[]) {
10 pattern = Pattern.compile("Hugo");
11 matcher = pattern.matcher("Hugo Etiévant");
12 while(matcher.find()) {
13 System.out.println("Trouvé !");
14 }
15 }
16}
1public class TestLambda {
2
3 public static void main(String args[]) {
4 List<String> lowerCaseStringsList = Arrays.asList("a","b","c");
5 // the :: notation is the lambda expression
6 // it's the same as the anonymous function s -> s.toUpperCase()
7 List<String> upperCaseStringsList = lowerCaseStringsList.stream().
8 map(String::toUpperCase).
9 collect(Collectors.toList());
10 }
11}