Regular expression matching

Pinterest LinkedIn Tumblr

Write a program to implement Regular expression matching. If you give the string and expression, the program should match the regular expression.

Algorithm Explanation

Initialize with string and use in build function to check space.
Split the string with space and print the string from the array of strings.
Validate whether the string has space and print the result.

Source Code

package com.dsacode.DataStructre.array;
public class RegularExpression {
          public static void main(String[] args) {
           String str = "Hello How are you";
           System.out.println("Does the string contain space: "+ str.matches("\\w.*"));
            String[] splitString = (str.split("\\s+"));
            System.out.println("Number of space between the string: "+ splitString.length);
            for (String string : splitString) {
              System.out.print(string +" ");
            System.out.println("Replace space with tab in the string:"+ str.replaceAll("\\s+", "\t"));


Does the string contain space: true
Number of space between the strings: 4
Hello How are you
Replace space with tab in the string: Hello How are you

Write A Comment