Print First Letter of Each Word Using Regex in Java



In this article, we will understand how to print the first letter of each word using regex. A regular expression is a sequence of characters that forms a search pattern. A regular expression can be a single character or a more complicated pattern.

A regular expression helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.

Problem Statement

Write a program in Java to print the first letter of each word using regex. Below is a demonstration of the same ?

Input 

Input String_1: Java Program
Input String_2: Joy of learning

Output

Result_1: JP
Result_2: Jol

Using main() method

Following are the steps to print the first letter of each word under the main() method ?

  • First, we will import the regex Matcher class and Pattern class using java.util.regex to use regular expressions.
  • Create two sample strings, input_string_1 and input_string_2, to apply the pattern to.
  • Define a regex pattern using Pattern.compile("\b[a-zA-Z]") to match the first letter of each word.
  • Use a Matcher object to apply the pattern on the strings. Loop through the matches and print the first letter of each word using the while loop.
  • Print the matched characters for each string, showing only the first letters of each word.

Example

Here, we bind all the operations together under the main() method ?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string_1 = "Java Program";
      System.out.println("\nThe first string is defined as: " +input_string_1);
      Pattern string_pattern = Pattern.compile("\b[a-zA-Z]");
      Matcher string_matcher = string_pattern.matcher(input_string_1);
      System.out.println("The first letters of the string is : ");
      while (string_matcher.find())
         System.out.print(string_matcher.group());
      System.out.println();
      String input_string_2 = "Joy of learning";
      System.out.println("\nThe second string is defined as: " +input_string_2);
      Matcher string_matcher_2 = string_pattern.matcher(input_string_2);
      System.out.println("The first letters of the string is : ");
      while (string_matcher_2.find())
         System.out.print(string_matcher_2.group());
      System.out.println();
   }
}

Output

Required packages have been imported

The first string is defined as: Java Program
The first letters of the string is :
JP

The second string is defined as: Java Program
The first letters of the string is :
Jol

Using encapsulation

Following are the steps to print the first letter of each word using object-oriented programming ?

  • First, we will import the Matcher class and Pattern class using java.util.regex to use regular expressions.
  • Create print_regex_string(String s), which takes a string as input and performs the regex operations to print the first letter of each word.
  • In print_regex_string(), use Pattern.compile() to match the first letter of each word.
  • Within the method, use Matcher to find each match and print the result.
  • Call print_regex_string() on each string, encapsulating the logic for finding and printing initials.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
   static void print_regex_string(String s) {
      Pattern string_pattern = Pattern.compile("\b[a-zA-Z]");
      Matcher string_matcher = string_pattern.matcher(s);
      System.out.println("The first letters of the string is : ");
      while (string_matcher.find())
         System.out.print(string_matcher.group());
      System.out.println();
   }
   public static void main(String[] args) {
      System.out.println("Required packages have been imported");
      String input_string_1 = "Java Program";
      System.out.println("\nThe first string is defined as: " +input_string_1);
      print_regex_string(input_string_1);
      String input_string_2 = "Joy of learning";
      System.out.println("\nThe second string is defined as: " +input_string_1);
      print_regex_string(input_string_2);
   }
}

Output

Required packages have been imported

The first string is defined as: Java Program
The first letters of the string is :
JP

The second string is defined as: Java Program
The first letters of the string is :
Jol
Updated on: 2024-10-29T00:38:40+05:30

683 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements