How to use intersection ([a-z&&[def]]) regular expression (REGEX) in Java

3 Answers

0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        // [a-z&&[def]] common to all nested classes d, e, or f
        searchREGEX("[a-z&&[def]]", "d", "a", "e", "f", "b");
    }
   	
    private static void searchREGEX(String regex, String... search) {
        Pattern pattern = Pattern.compile(regex);
	    for (String input : search) {
            Matcher matcher = pattern.matcher(input);
 
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("Found" + " \"%s\" " + "from index %d to %d",
                                            matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            if (!found) 
                System.out.println("Not found");
	    }
    }
}


/*
run:
 
Found "d" from index 0 to 1
Not found
Found "e" from index 0 to 1
Found "f" from index 0 to 1
Not found
 
*/

 



answered Jan 19, 2016 by avibootz
edited Jan 19, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        // [0-9&&[678]] common to all nested classes 6, 7, or 8
        searchREGEX("[0-9&&[678]]", "7", "2", "4", "6", "8", "9");
    }
   	
    private static void searchREGEX(String regex, String... search) {
        Pattern pattern = Pattern.compile(regex);
	    for (String input : search) {
            Matcher matcher = pattern.matcher(input);
 
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("Found" + " \"%s\" " + "from index %d to %d",
                                            matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            if (!found) 
                System.out.println("Not found");
	    }
    }
}


/*
run:
 
Found "7" from index 0 to 1
Not found
Not found
Found "6" from index 0 to 1
Found "8" from index 0 to 1
Not found
 
*/

 



answered Jan 19, 2016 by avibootz
edited Jan 19, 2016 by avibootz
0 votes
package javaapplication1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        // [1-7&&[4-8]] common to all nested classes 4, 5, 6 or 7
        searchREGEX("[1-7&&[4-8]]", "1", "3", "6", "4", "5", "7", "8");
    }
   	
    private static void searchREGEX(String regex, String... search) {
        Pattern pattern = Pattern.compile(regex);
	    for (String input : search) {
            Matcher matcher = pattern.matcher(input);
 
            boolean found = false;
            while (matcher.find()) {
               System.out.println(String.format("Found" + " \"%s\" " + "from index %d to %d",
                                            matcher.group(), matcher.start(), matcher.end()));
               found = true;
            }
            if (!found) 
                System.out.println("Not found");
	    }
    }
}


/*
run:
 
Not found
Not found
Found "6" from index 0 to 1
Found "4" from index 0 to 1
Found "5" from index 0 to 1
Found "7" from index 0 to 1
Not found
 
*/

 



answered Jan 19, 2016 by avibootz

Related questions

1 answer 125 views
1 answer 200 views
1 answer 182 views
1 answer 237 views
1 answer 269 views
...