How to use subtraction ([a-z&&[^m-p]] and [a-z&&[^bc]]) 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&&[^m-p]] a through z, and not m through p
        searchREGEX("[a-z&&[^m-p]]", "a", "b", "c", "m", "n", "o", "p", "z");
    }
   	
    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 "a" from index 0 to 1
Found "b" from index 0 to 1
Found "c" from index 0 to 1
Not found
Not found
Not found
Not found
Found "z" from index 0 to 1
 
*/

 



answered Jan 20, 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) {
        // [a-z&&[^bc]] a through z, except for b and c
        searchREGEX("[a-z&&[^bc]]", "a", "b", "c", "e", "f", "o", "p", "z");
    }
   	
    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 "a" from index 0 to 1
Not found
Not found
Found "e" from index 0 to 1
Found "f" from index 0 to 1
Found "o" from index 0 to 1
Found "p" from index 0 to 1
Found "z" from index 0 to 1
 
*/

 



answered Jan 20, 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&&[^456]] 0 through 9, except for 4, 5 and 6
        searchREGEX("[0-9&&[^456]]", "1", "3", "4", "5", "7", "6", "9", "0");
    }
   	
    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 "1" from index 0 to 1
Found "3" from index 0 to 1
Not found
Not found
Found "7" from index 0 to 1
Not found
Found "9" from index 0 to 1
Found "0" from index 0 to 1
 
*/

 



answered Jan 20, 2016 by avibootz

Related questions

2 answers 281 views
1 answer 125 views
1 answer 200 views
1 answer 182 views
...