捕获匹配的正则表达式和替代

时间:2016-08-03 11:26:34

标签: c regex pcre c-strings

我有一个C程序来匹配字符串中的特定正则表达式并执行一些替换。我希望匹配所有字符串连接模式并将其转换为另一种形式。

输入字符串的示例

This "is" + "an" + "example" + "string" to be matched and replaced

匹配的正则表达式应该是

"is" + "an" + "example" + "string"

所以我想把这个匹配的正则表达式转换为

CONCAT("is", "an", "example", "string")

我正在使用C PCRE2库,我的代码如下。任何人都可以帮我修理替换部分吗?

#define PCRE2_CODE_UNIT_WIDTH 8
#define BUFFERSIZE 100
#include <my_global.h>
#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <pcre2.h>

void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s\n", mysql_error(con));
  mysql_close(con);
  exit(1);        
}


int main(int argc, char **argv)
{
pcre2_code *re;
PCRE2_SPTR pattern;     
PCRE2_SPTR subject;     

int errornumber;
int i;
int rc;


PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;

size_t subject_length;
pcre2_match_data *match_data;

 char buffer[BUFFERSIZE];
 MYSQL *con = mysql_init(NULL);


/* Read Query String from standard input here*/

printf("Enter a Query to be substituted: \n");
    while(fgets(buffer, BUFFERSIZE , stdin) != NULL)
    {
        printf("%s\n", buffer);
        break;
    }

pattern = (PCRE2_SPTR)'"[^"]*"(?: *\\+ *"[^"]*")+';   // Regex for string concatenation
subject = (PCRE2_SPTR)buffer;
subject_length = strlen((char *)subject);


/* Compile regex and perform matching here  by wrapping match within CONCAT() and replace + with , */


re = pcre2_compile(
  pattern,               
  PCRE2_ZERO_TERMINATED,
  0,                     
  &errornumber,          
  &erroroffset,         
  NULL);                 


if (re == NULL)
  {
  PCRE2_UCHAR buffer[256];
  pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
  printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,
    buffer);
  return 1;
  }



match_data = pcre2_match_data_create_from_pattern(re, NULL);

rc = pcre2_match(
  re,                   
  subject,              
  subject_length,      
  0,                    
  0,                   
  match_data,          
  NULL);  


if (rc < 0)
  {
  switch(rc)
    {
    case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
    default: printf("Matching error %d\n", rc); break;
    }
  pcre2_match_data_free(match_data);   
  pcre2_code_free(re);                
  // return 1;
  goto server;
  }


ovector = pcre2_get_ovector_pointer(match_data);
printf("\nMatch succeeded at offset %d\n", (int)ovector[0]);


/* Do Substitution for matches found here. by wrapping match within CONCAT() and replace + with ,.  For now only one match is handled*/


/* Use Connector C here to forward query that is rewritten to MariaDB/MySQL server */
server:
if (con == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }
  if (mysql_real_connect(con, "localhost", "root", "root", 
          NULL, 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }  
  printf("Connected to Database\n");


  if (mysql_query(con, subject)) 
  {
      finish_with_error(con);
  }

  MYSQL_RES *result = mysql_store_result(con);

  if (result == NULL) 
  {
      finish_with_error(con);
  }

  int num_fields = mysql_num_fields(result);

  MYSQL_ROW row;

  while ((row = mysql_fetch_row(result))) 
  { 
      for(int i = 0; i < num_fields; i++) 
      { 
          printf("%s ", row[i] ? row[i] : "NULL"); 
      } 
          printf("\n"); 
  }

  mysql_free_result(result);

  mysql_close(con);

printf("\n");
pcre2_match_data_free(match_data);
pcre2_code_free(re);
return 0;
}

0 个答案:

没有答案