Welcome to Kodejava

Kodejava website provides beginners to Java programming some examples to use the Java API (Application Programming Interface) to develop applications. Learning from some examples will hopefully decrease the time required to learn Java.

In this website you will find a lot of examples which grouped by the Java API package. You can easily find a solution to your problem and it's free. Enjoy your study, come and visit the site regularly to find more and more examples of Java code.

You can also fork the examples published on this website from our GitHub website on the following address: https://github.com/wsaryada/kodejava

--
I Wayan Saryada
Kodejava Webmaster

Java is one of the many languages supported as standard on hosting platforms like iPage. You do not need to know Java to build a website, as demonstrated in Ian's video training for iPage.

How do I write union character class regex?

To create a single character class comprised of two or more separate character classes use unions. To create a union, simply nest one class inside the other, such as [1-3[5-7]].

package org.kodejava.example.util.regex;

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

public class CharacterClassUnionDemo {
    public static void main(String[] args) {
        //
        // Defines regex that matches the number 1, 2, 3, 5, 6, 7
        //
        String regex = "[1-3[5-7]]";
        String input = "1234567890";

        //
        // Compiles the given regular expression into a pattern.
        //
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        //
        // Find matches and print it
        //
        while (matcher.find()) {
            System.out.format("Text \"%s\" found at %d to %d.%n",
                    matcher.group(), matcher.start(), 
                    matcher.end());
        }


    }
}

Here is the result of the program:

Text "1" found at 0 to 1.
Text "2" found at 1 to 2.
Text "3" found at 2 to 3.
Text "5" found at 4 to 5.
Text "6" found at 5 to 6.
Text "7" found at 6 to 7.