How to Convert a Java String Against a Pattern Regex? Last Updated : 29 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Regular expressions, or "regex," are an effective tool in Java programming for checking texts against a given pattern. This post will walk you through the process of utilizing regular expressions to validate a Java text against a pattern. Prerequisites:String in JavaRegular Expressions in JavaConvert Java String against Pattern RegexFormat or String structure using a regular expression pattern is used here. The Matcher class is responsible for comparing the pattern with the input string. If the full string fits the pattern, the Matcher class's matches() function returns true. Java // Java Program to Convert // Java String against Pattern Regex import java.util.regex.*; // Driver Class public class Temp { // Method to check pattern static void checkPattern(String testString, String regex) { // Split the string based on whitespaces String[] words = testString.split("\\s+"); // Compile the pattern Pattern pattern = Pattern.compile(regex); for (String word : words) { Matcher matcher = pattern.matcher(word); // Check if the string matches the pattern if (!matcher.matches()) { System.out.print(word + " "); } } System.out.println(); } // Main Function public static void main(String[] args) { String testString = "I am in GFG. GFG is a good company and I like to work in it"; // Define a simple regex pattern // Regex for GFG String regex = ".*\\bGFG\\b.*"; // Call the checkPattern method checkPattern(testString, regex); } } OutputI am in is a good company and I like to work in it Comment More infoAdvertise with us Next Article Java Program to Convert String to String Array Using Regular Expression R rahul9yw89 Follow Improve Article Tags : Java Java Programs java-regular-expression Java-String-Programs regular-expression Java Examples +2 More Practice Tags : Java Similar Reads How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Capitalize the First Letter of a String in Java Using Regex In Java, we can use regular expressions to make the first character of each word in Uppercase as the word is represented as a String and string is the collection of characters. So, in Java, we can use pattern and matcher class methods to find the first character of a word and then replace it with it 2 min read Java Program to Extract an HTML Tag from a String using RegEx In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequen 3 min read Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Java Program to Convert String to String Array Using Regular Expression Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided un 3 min read How to Match Phone Numbers in a List to a Regex Pattern in Java ? Regular Expressions are also known as regex or regexp. It is one of the powerful features of Java that helps in sequences of characters that define a search pattern. Nowadays it is widely used in computer science, programming, and text processing for tasks such as string matching, data extraction, a 4 min read Like