خرید بک لینک

Vote count: 0

I have a C program to match a particular regex in a string and perform some substitution. I want to match all pattes of string concatenation and convert it to another form.

For Example for the input string

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

The Matched regex should be

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

So I want to convert this matched regex to

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

I am using C PCRE2 Library and my code is below. Can anyone help me fix the substitution part?

#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, "%sn", mysql_error(con));
  mysql_close(con);
  exit(1);        
}


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

int erroumber;
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("%sn", buffer);
        break;
    }

patte = (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(
  patte,               
  PCRE2_ZERO_TERMINATED,
  0,                     
  &erroumber,          
  &erroroffset,         
  NULL);                 


if (re == NULL)
  {
  PCRE2_UCHAR buffer[256];
  pcre2_get_error_message(erroumber, buffer, sizeof(buffer));
  printf("PCRE2 compilation failed at offset %d: %sn", (int)erroroffset,
    buffer);
  retu 1;
  }



match_data = pcre2_match_data_create_from_patte(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 matchn"); break;
    default: printf("Matching error %dn", rc); break;
    }
  pcre2_match_data_free(match_data);   
  pcre2_code_free(re);                
  // retu 1;
  goto server;
  }


ovector = pcre2_get_ovector_pointer(match_data);
printf("nMatch succeeded at offset %dn", (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 Coector C here to forward query that is rewritten to MariaDB/MySQL server */
server:
if (con == NULL) 
  {
      fprintf(stderr, "%sn", mysql_error(con));
      exit(1);
  }
  if (mysql_real_coect(con, "localhost", "root", "root", 
          NULL, 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%sn", mysql_error(con));
      mysql_close(con);
      exit(1);
  }  
  printf("Coected to Databasen");


  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);
retu 0;
}

asked 11 secs ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 13 مرداد 1395 ساعت: 18:40

صفحه بندی