GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java Pattern.flags Method - Complete Tutorial with Examples Java Pattern.flags MethodLast modified: April 20, 2025
The Flags can be combined using bitwise OR operations to enable multiple behaviors simultaneously. The method returns an integer representing the bitmask of all enabled flags for the Pattern instance. Pattern Flags Overview
Java's Pattern class provides several flags that alter regex matching behavior.
Common flags include
The Basic flags Method Usage
This example demonstrates the basic usage of the PatternFlagsBasic.java
package com.zetcode;
import java.util.regex.Pattern;
public class PatternFlagsBasic {
public static void main(String[] args) {
// Pattern with no flags
Pattern noFlags = Pattern.compile("abc");
System.out.println("No flags: " + noFlags.flags());
// Pattern with CASE_INSENSITIVE flag
Pattern caseInsensitive = Pattern.compile("abc", Pattern.CASE_INSENSITIVE);
System.out.println("CASE_INSENSITIVE: " + caseInsensitive.flags());
// Pattern with multiple flags
Pattern multiFlags = Pattern.compile("abc",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
System.out.println("Multiple flags: " + multiFlags.flags());
}
}
In this example, we create three Pattern instances with different flag
combinations. The The output will show the numeric values of the flag combinations. These values can be compared with the Pattern class constants to determine which flags are set. Checking Individual FlagsThis example shows how to check for specific flags using the flags() method return value. We'll use bitwise operations to test which flags are enabled. PatternCheckFlags.java
package com.zetcode;
import java.util.regex.Pattern;
public class PatternCheckFlags {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-z]+$",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
int flags = pattern.flags();
System.out.println("All flags: " + flags);
System.out.println("CASE_INSENSITIVE enabled: " +
((flags & Pattern.CASE_INSENSITIVE) != 0));
System.out.println("MULTILINE enabled: " +
((flags & Pattern.MULTILINE) != 0));
System.out.println("DOTALL enabled: " +
((flags & Pattern.DOTALL) != 0));
System.out.println("UNICODE_CASE enabled: " +
((flags & Pattern.UNICODE_CASE) != 0));
}
}
Here we create a Pattern with CASE_INSENSITIVE and MULTILINE flags. We then use bitwise AND operations to check which individual flags are set in the returned flags value. The output demonstrates how to verify the presence of specific flags in the Pattern's configuration. This technique is useful for conditional logic based on Pattern settings. Reconstructing Pattern with Same Flags
This example shows how to use the PatternReuseFlags.java
package com.zetcode;
import java.util.regex.Pattern;
public class PatternReuseFlags {
public static void main(String[] args) {
Pattern original = Pattern.compile("^start.*end$",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
// Get flags from original pattern
int flags = original.flags();
// Create new pattern with same flags but different regex
Pattern modified = Pattern.compile("^begin.*finish$", flags);
System.out.println("Original flags: " + original.flags());
System.out.println("Modified flags: " + modified.flags());
// Verify both patterns have same flags
System.out.println("Flags match: " +
(original.flags() == modified.flags()));
}
}
In this example, we first create a This technique ensures consistent matching behavior across different patterns when flag settings need to be preserved. Flags with Pattern.quote
This example demonstrates how flags interact with PatternQuoteFlags.java
package com.zetcode;
import java.util.regex.Pattern;
public class PatternQuoteFlags {
public static void main(String[] args) {
String regex = "[a-z]+";
Pattern normalPattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
// Create quoted pattern
Pattern quotedPattern = Pattern.compile(Pattern.quote(regex));
System.out.println("Normal pattern flags: " + normalPattern.flags());
System.out.println("Quoted pattern flags: " + quotedPattern.flags());
// Attempt to add flags to quoted pattern
Pattern quotedWithFlags = Pattern.compile(Pattern.quote(regex),
Pattern.CASE_INSENSITIVE);
System.out.println("Quoted with flags: " + quotedWithFlags.flags());
}
}
This example shows that
The output demonstrates that quoted patterns maintain their Flags and Pattern.split
This example examines how flags affect the PatternSplitFlags.java
package com.zetcode;
import java.util.regex.Pattern;
public class PatternSplitFlags {
public static void main(String[] args) {
String input = "Apple\nBanana\nCherry";
String regex = "^";
// Split with MULTILINE flag
Pattern multiline = Pattern.compile(regex, Pattern.MULTILINE);
String[] multilineResult = multiline.split(input);
System.out.println("Multiline split count: " + multilineResult.length);
System.out.println("Multiline flags: " + multiline.flags());
// Split without MULTILINE flag
Pattern normal = Pattern.compile(regex);
String[] normalResult = normal.split(input);
System.out.println("Normal split count: " + normalResult.length);
System.out.println("Normal flags: " + normal.flags());
}
}
In this example, we demonstrate how the MULTILINE flag affects the
The Flags Inheritance in Pattern.matcher
This example shows that PatternMatcherFlags.java
package com.zetcode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternMatcherFlags {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-z]+$",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher matcher = pattern.matcher("Test");
// Verify matcher uses pattern's flags
System.out.println("Pattern flags: " + pattern.flags());
System.out.println("Does 'Test' match? " + matcher.matches());
// Change would require new Pattern with different flags
Pattern noFlags = Pattern.compile("^[a-z]+$");
Matcher strictMatcher = noFlags.matcher("Test");
System.out.println("No flags pattern: " + noFlags.flags());
System.out.println("Does 'Test' match now? " + strictMatcher.matches());
}
}
This example demonstrates that
The output shows how different flag settings affect matching behavior and how
the SourceJava Pattern.flags Documentation
In this article, we've explored the AuthorMy name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |