logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Implement Regular Expression Matching

Posted on May 25, 2013 by staff10
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.

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

If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Find the Length of the Longest Repeating Sequence in a String
» Next Page - C Program to Convert the Content of File to UpperCase
« C Program to Find the Length of the Longest Repeating Sequence in a String
C Program to Convert the Content of File to UpperCase »

Deep Dive @ Sanfoundry:

  1. C++ Programming Examples on Data-Structures
  2. C++ Programming Examples on Graph Problems & Algorithms
  3. C Programming Examples on Strings
  4. C# Programming Examples on Functions
  5. C# Programming Examples on Interfaces
  6. Java Programming Examples on Collection API
  7. C Programming Examples on Graph Problems & Algorithms
  8. C# Programming Examples on Delegates
  9. C++ Programming Examples on Set & String Problems & Algorithms
  10. C Programming Examples on Set & String Problems & Algorithms
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry