Match Expression where a single special character in pattern can match one or more characters
Last Updated :
03 May, 2021
Given two string, in which one is a pattern (Pattern) and the other is a searching expression. Searching expression contains ‘#’.
The # works in the following way:
- A # matches with one or more characters.
- A # matches all characters before a pattern match is found. For example, if pat = “A#B”, and text is “ACCBB”, then # would match only with “CC” and pattern is considered as not found.
Examples :
Input : str = "ABABABA"
pat = "A#B#A"
Output : yes
Input : str = "ABCCB"
pat = "A#B"
Output : yes
Input : str = "ABCABCCE"
pat = "A#C#"
Output : yes
Input : str = "ABCABCCE"
pat = "A#C"
Output : no
We can observe that whenever we encounter ‘#’, we have to consider as many characters till the next character of the pattern will not be equal to the current character of the given string. Firstly, we check if the current character of the pattern is equal to ‘#’-
a) If not then we check whether the current character of string and pattern are the same or not, if same, then increment both counters else return false from here only. No need for further checking.
b) If yes, then we have to find the position of a character in the text that matches with the next character of the pattern.
C++
#include<bits/stdc++.h>
using namespace std;
int regexMatch(string text, string pat)
{
int lenText = text.length();
int letPat = pat.length();
int i = 0, j = 0;
while (i < letPat)
{
if (pat[i] != '#' )
{
if (pat[i] != text[j])
return false ;
i++;
j++;
}
else
{
j++;
while (text[j] != pat[i + 1])
j++;
i++;
}
}
return (j == lenText);
}
int main()
{
string str = "ABABABA" ;
string pat = "A#B#A" ;
if (regexMatch(str, pat))
cout << "yes" ;
else
cout << "no" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static boolean regexMatch
(String text, String pat)
{
int lenText = text.length();
int lenPat = pat.length();
char [] Text = text.toCharArray();
char [] Pat = pat.toCharArray();
int i = 0 , j = 0 ;
while (i < lenPat)
{
if (Pat[i] != '#' )
{
if (Pat[i] != Text[j])
return false ;
i++;
j++;
}
else
{
j++;
while (Text[j] != Pat[i + 1 ])
j++;
i++;
}
}
return (j == lenText);
}
public static void main (String[] args)
{
String str = "ABABABA" ;
String pat = "A#B#A" ;
if (regexMatch(str, pat))
System.out.println( "yes" );
else
System.out.println( "no" );
}
}
|
Python3
def regexMatch(text, pat):
lenText = len (text)
letPat = len (pat)
i = 0
j = 0
while (i < letPat):
if (pat[i] ! = '#' ):
if (pat[i] ! = text[j]):
return False
i + = 1
j + = 1
else :
j + = 1
while (text[j] ! = pat[i + 1 ]):
j + = 1
i + = 1
return (j = = lenText)
if __name__ = = "__main__" :
st = "ABABABA"
pat = "A#B#A"
if (regexMatch(st, pat)):
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
class GFG
{
public static bool regexMatch
(String text, String pat)
{
int lenText = text.Length;
int lenPat = pat.Length;
char []Text = text.ToCharArray();
char []Pat = pat.ToCharArray();
int i = 0, j = 0;
while (i < lenPat)
{
if (Pat[i] != '#' )
{
if (Pat[i] != Text[j])
return false ;
i++;
j++;
}
else
{
j++;
while (Text[j] != Pat[i + 1])
j++;
i++;
}
}
return (j == lenText);
}
public static void Main ()
{
String str = "ABABABA" ;
String pat = "A#B#A" ;
if (regexMatch(str, pat))
Console.Write( "yes" );
else
Console.Write( "no" );
}
}
|
PHP
<?php
function regexMatch( $text , $pat )
{
$lenText = strlen ( $text );
$letPat = strlen ( $pat );
$i = 0; $j = 0;
while ( $i < $letPat )
{
if ( $pat [ $i ] != '#' )
{
if ( $pat [ $i ] != $text [ $j ])
return false;
$i ++;
$j ++;
}
else
{
$j ++;
while ( $text [ $j ] != $pat [ $i + 1])
$j ++;
$i ++;
}
}
return ( $j == $lenText );
}
$str = "ABABABA" ;
$pat = "A#B#A" ;
if (regexMatch( $str , $pat ))
echo "yes" ;
else
echo "no" ;
?>
|
Javascript
<script>
function regexMatch(text,pat)
{
let lenText = text.length;
let lenPat = pat.length;
let Text = text.split( "" );
let Pat = pat.split( "" );
let i = 0, j = 0;
while (i < lenPat)
{
if (Pat[i] != '#' )
{
if (Pat[i] != Text[j])
return false ;
i++;
j++;
}
else
{
j++;
while (Text[j] != Pat[i + 1])
j++;
i++;
}
}
return (j == lenText);
}
let str = "ABABABA" ;
let pat = "A#B#A" ;
if (regexMatch(str, pat))
document.write( "yes" );
else
document.write( "no" );
</script>
|
Output:
yes
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...