Search for string matching using regular expressions in C Language

Source: Internet
Author: User
/* Question 1) Introduction input: Regular Expression string to be matched string, if the input "######" program ends, output: If the matching is successful, the entire string is output, if the match fails, output the lost and wait for the next user input. ^ Indicates the start of the string. represents any character $ represents the end of the string 2) reference input, output ^. $ abc response regular express and target string (separate by space), inputlost response result, outputab. $ ababc release regular express and target string (separate by space), inputababc match successfully, print the target string, output ###### complete input ##### end the entire program */# include <stdio. h> # include <string. h> # define X 20int main () {int I, j, x1, x2, u, flag, ret, w; char str1 [X], str2 [X]; for (u = 0; u ++) {scanf ("% s", str1); scanf ("% s", str2); x1 = strlen (str1 ); x2 = strlen (str2); flag = 0; if (x1 = 3 & x2 = 3) {for (I = 0; I <3; I ++) {if (str1 [I]! = '#' | Str2 [I]! = '#') Flag ++;} if (flag = 0) return 0;} if (str1 [0] = '^ ') /* 1 & 2 */{if (str1 [x1-1] = '$') {if (x1 = x2 + 2) for (I = 1; I <x1-1; I ++) {if (str1 [I]! = '.') {If (str1 [I]! = Str2 [I-1]) {printf ("lost \ n"); break;} else {if (I = x1-2) {puts (str2); break ;}}} else continue;} else printf ("lost \ n");} else/* 2 */{if (x2 <x1-1) printf ("lost \ n "); else for (I = 1; I <x1; I ++) {if (str1 [I]! = '.') {If (str1 [I]! = Str2 [I-1]) {printf ("lost \ n"); break;} else if (I = x1-1) {puts (str2); break ;}} else continue;} else/* 3 & 4 */{if (str1 [x1-1] = '$')/* 3 */{if (x2 <x1-1) printf ("lost \ n"); else {for (I = x1-2; I> = 0; I --) {if (str1 [I]! = '.') {If (str1 [I]! = Str2 [x2-x1 + I + 1]) {printf ("lost \ n"); break;} else {if (I = 0) {puts (str2 ); break ;}}}} else/* 4 */{ret = 0; if (x1> x2) printf ("lost \ n "); else {for (w = 0; w <x1; w ++) if (str1 [w]! = 0) break; for (j = w; j <x2; j ++) {if (str1 [w] = str2 [j]) for (I = 1; I <x1-w; I ++) {if (str1 [I + w]! = '.') {If (str1 [I + w]! = Str2 [I + j]) break; else {if (I + w = x1-1) {ret = 1; puts (str2 );}}}} if (ret = 1) break;} if (ret! = 1) printf ("lost \ n") ;}}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.