C Program to Implement Regular Expression Matching

This C Program implements regular expression matching.

Here is source code of the C Program to implement regular expression matching. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implements Regular Expression Matching
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #define MATCH printf("\nThe Text Matches The Regular Expression");
  7. #define NOTMATCH printf("\nThe Text Doesn't match the Regular Expression");
  8.  
  9. char reg[20], text[20];
  10.  
  11. int main()
  12. {
  13.     int i, rlen, tlen, f = 0;
  14.     char ans;
  15.  
  16.     do {
  17.             printf("\nEnter the Regular Expression\n");    
  18.             scanf(" %[^\n]s", reg);
  19.             for (rlen = 0; reg[rlen] != '\0';rlen++);
  20.             printf("\nEnter the text\n");
  21.             scanf(" %[^\n]s", text);
  22.             for (tlen = 0;text[tlen] != '\0' ; tlen++);
  23.             if (reg[0] == '*')
  24.             {
  25.                 printf("\nInvalid regular expression");
  26.             }
  27.             /*
  28.              *If the regular expression starts with Alphabet
  29.              */
  30.             if ((reg[0] >= 65 && reg[0] <= 90) || (reg[0] >= 97 && reg[0] <=122))
  31.             {
  32.                 if (reg[0] == text [0])
  33.                 {
  34.                     switch (reg[1]) 
  35.                     {
  36.                     case '.' :
  37.                         switch (reg[2])
  38.                         {
  39.                         case '*':
  40.                             if (tlen != 1)
  41.                             {
  42.                                 if (reg[3] == text[tlen-1])
  43.                                 {
  44.                                     MATCH;
  45.                                 }
  46.                                 else
  47.                                 {
  48.                                     NOTMATCH;
  49.                                 }
  50.                             }
  51.                             else
  52.                             {
  53.                                 NOTMATCH;
  54.                             }
  55.                             break;
  56.                         case '+':
  57.                             if (text[1] != reg[3])
  58.                             {
  59.                                 if (reg[3] == text[tlen - 1])
  60.                                 {
  61.                                     MATCH;
  62.                                 }
  63.                                 else
  64.                                 {
  65.                                     NOTMATCH;
  66.                                 }
  67.                             }
  68.                             break;
  69.                         case '?':
  70.                             if (text[1] == reg[3] || text[2] == reg[3])
  71.                             {
  72.                                 if (text[1] == reg[3] || text[2] == reg[3])
  73.                                 {
  74.                                     MATCH;
  75.                                 }
  76.                                 else
  77.                                 {
  78.                                     NOTMATCH;
  79.                                 }
  80.                             }
  81.                             else
  82.                             {
  83.                                 NOTMATCH;
  84.                             }
  85.                              break;
  86.                             }
  87.                             break;
  88.                         case '*':
  89.                             if (reg[rlen-1] == text[tlen-1])
  90.                             {
  91.                                 for (i = 0;i <= tlen-2;i++)
  92.                                 {
  93.                                     if(text[i] == reg[0])
  94.                                     {
  95.                                         f = 1;        
  96.                                     }
  97.                                     else 
  98.                                     {
  99.                                         f = 0;
  100.                                     }
  101.                                 }
  102.                                 if ( f == 1)
  103.                                 {
  104.                                     MATCH;
  105.                                 }
  106.                                 else
  107.                                 {
  108.                                     NOTMATCH;
  109.                                 }
  110.                             }
  111.                             else
  112.                             {
  113.                                 NOTMATCH;
  114.                             }
  115.                             break;
  116.                     case '+' :
  117.                         if (tlen <= 2)
  118.                         {    
  119.                             NOTMATCH;
  120.                         }
  121.                         else if (reg[rlen-1] == text[tlen-1])
  122.                         {
  123.                             for (i = 0;i < tlen-2;i++)
  124.                             {
  125.                                 if (text[i] == reg[0])
  126.                                 {
  127.                                     f = 1;
  128.                                 }
  129.                                 else
  130.                                 {
  131.                                     f = 0;
  132.                                 }
  133.                             }
  134.  
  135.                             if (f == 1)
  136.                             {
  137.                                 MATCH;
  138.                             }
  139.                             else 
  140.                             {
  141.                                 NOTMATCH;
  142.                             }
  143.                         }
  144.                             break;
  145.                     case '?':
  146.                         if (reg[rlen -1] == text[tlen-1])
  147.                         {
  148.                             MATCH;
  149.                         }
  150.                         else
  151.                         {
  152.                             NOTMATCH;
  153.                         }
  154.                     break;
  155.                 } 
  156.  
  157.             }
  158.             else
  159.                 printf("Does not match");             
  160.         }
  161.         /*
  162.          *If Regular Expression starts with '^'
  163.          */
  164.         else if (reg[0] == '^')
  165.         {
  166.             if (reg[1] == text[0])
  167.             {
  168.                 MATCH;
  169.             }
  170.             else
  171.             {
  172.                 NOTMATCH;
  173.             }
  174.         }
  175.         /*
  176.          *If Regular Expression Ends with '$'
  177.          */
  178.         else if (reg[rlen-1] == '$')
  179.         {
  180.             if (reg[rlen-2] == text[rlen-1])
  181.             {
  182.                 MATCH;
  183.             }
  184.             else
  185.             { 
  186.                 NOTMATCH;
  187.             }
  188.         }
  189.  
  190.         else
  191.             printf("Not Implemented");
  192.         printf("\nDo you want to continue?(Y/N)");
  193.         scanf(" %c", &ans);
  194.     } while (ans == 'Y' || ans == 'y');
  195. }

$gcc -o regex regular.c
$ ./regex
 
Enter the Regular Expression
C.*g
 
Enter the text
Cprogramming
 
The Text Matches The Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C*g
 
Enter the text
Cprogramming
 
The Text Doesn't match the Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C?.*g
 
Enter the text
Cprogramming
 
The Text Matches The Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C.?g
 
Enter the text
Cprogramming
 
The Text Doesn't match the Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C.+g
 
Enter the text
Cprogramming
 
The Text Matches The Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C+g
 
Enter the text
Cprogramming
 
The Text Doesn't match the Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
 
^C.*
 
Enter the text
Cprogramming
 
The Text Matches The Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
^p.*
 
Enter the text
Cprogramming
 
The Text Doesn't match the Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C.*g$
 
Enter the text
Cprogramming
 
The Text Matches The Regular Expression
Do you want to continue?(Y/N)y
 
Enter the Regular Expression
C.*n$
 
Enter the text
Cprogramming
 
The Text Doesn't match the Regular Expression
Do you want to continue?(Y/N)n

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.