1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   | // Nicolas_75, 28 août 2006
// http://www.developpez.net/forums/showthread.php?t=198069
// appeler le fichier TestRegexSubnet.java
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class TestRegexSubnet {
 
    public static void main(String[] args) {
 
        Pattern pattern = Pattern.compile("subnet(\\s+)(\\w{1,3}.\\w{1,3}.\\w{1,3}.\\w{1,3})");
        // dans ce cas, l'adresse IP est récupérée par myMatcher.group(2);
 
        Matcher m;
        String toBeTested;
 
        toBeTested = "subnet 192.168.0.0 netmask 255.255.255.0";
        m = pattern.matcher(toBeTested);
        if (m.find()) {
            System.out.println(toBeTested+" => >"+m.group(2)+"<");
        }
        else {
            System.out.println(toBeTested+" => pas d'occurrence trouvée");
        }
 
        toBeTested = "option subnet-mask 255.255.255.0";
        m = pattern.matcher(toBeTested);
        if (m.find()) {
            System.out.println(toBeTested+" => >"+m.group(2)+"<");
        }
        else {
            System.out.println(toBeTested+" => pas d'occurrence trouvée");
        }
    }
}
 
// RESULTAT :
// subnet 192.168.0.0 netmask 255.255.255.0 => >192.168.0.0<
// option subnet-mask 255.255.255.0 => pas d'occurrence trouvée | 
Partager