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}}