Thursday, September 4, 2025
HomeLanguagesJavaPattern flags() method in Java with Examples

Pattern flags() method in Java with Examples

The flags() method of the Pattern class in Java is used to return the pattern’s match flags. The Match flags are a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS Flags. 

Syntax:

public int flags()

Parameters: This method does not accepts any parameters. 

Return Value: This method returns the pattern’s match flag. 

Below programs illustrate the flags() method: 

Program 1: 

Java




// Java program to demonstrate
// Pattern.flags() method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "(.*)(for)(.*)?";
 
        // create the string
        // in which you want to search
        String actualString
            = "code of Machine";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX,
                         Pattern.CASE_INSENSITIVE);
 
        // find the flag of pattern
        int flag = pattern.flags();
 
        System.out.println("Pattern's match flag = "
                           + flag);
    }
}


Output:

Pattern's match flag = 2

Program 2: 

Java




// Java program to demonstrate
// Pattern.flags method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create a REGEX String
        String REGEX = "(.*)(ee)(.*)?";
 
        // create the string
        // in which you want to search
        String actualString
            = "geeks";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX,
                                 Pattern.MULTILINE);
 
        // find the flag of pattern
        int flag = pattern.flags();
 
        System.out.println("Pattern's match flag = "
                           + flag);
    }
}


Output:

Pattern's match flag = 8

References: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#flags()

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS