來源:https ://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
我試圖在 C 中模仿以下 2 個 SQL 查詢。第一個有效;第二次失敗:
SELECT regexp_match('hello world test', 'world.{3}');
SELECT regexp_match('foobarbequebaz', '(bar)(beque)');
#include<regex.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_MATCHES 1024
int main(void)
{
regex_t regex;
int reti;
char msgbuf[100];
char buff0[20];
char buff[20];
char buff1[20];
char *sz1 = "hello world test";
//char *sz2= "foobarbequebaz";
char *pattern1 = "world.{3}";
//char *pattern2 = "(bar)(beque)";
regmatch_t matches[MAX_MATCHES];
/* Compile regular expression */
reti = regcomp(®ex,pattern1,REG_EXTENDED);
if(reti){
fprintf(stderr,"could not compile\n");
exit(EXIT_FAILURE);
}
reti = regexec(®ex,sz1,MAX_MATCHES,matches,0);
if(!reti){
printf("szso=%d\n",matches[1].rm_so);
printf("szeo=%d\n",matches[1].rm_eo);
memcpy(buff0,sz1 matches[0].rm_so,matches[0].rm_eo-matches[0].rm_so);
memcpy(buff,sz1 matches[1].rm_so,matches[1].rm_eo-matches[1].rm_so);
memcpy(buff1,sz1 matches[2].rm_so,matches[2].rm_eo-matches[2].rm_so);
printf("group0: %s\n",buff0);
printf("group1: %s\n",buff);
printf("group2: %s\n",buff1);
}
else if(reti == REG_NOMATCH){
puts("No match");
}
else{
regerror(reti,®ex,msgbuf,sizeof(msgbuf));
fprintf(stderr,"Regex match failed: %s\n",msgbuf);
exit(EXIT_FAILURE);
}
regfree(®ex);
exit(EXIT_SUCCESS);
}
輸出
szso=3
szeo=11
group1: barbeque
期望兩組,所以 group1 只回傳bar。
更新問題:
pattern2再次匹配sz2預期的行為。- 但是,如果只有模式的一部分匹配,則
matches[0]應該與matches[1]. - 所以在這個新的背景下,我應該期望
group0是一樣的group1嗎?
uj5u.com熱心網友回復:
閱讀手冊頁。matches[0]包含整個匹配,matches[1]第一個帶括號的組,matches[2]第二個,依此類推。
uj5u.com熱心網友回復:
請注意,它memcpy()為您提供了一個位元組陣列,而不是一個字串。如果沒有一些額外的作業,您將無法buff在printf()陳述句中可靠地使用。你可以使用:
int i = 0;
int len = matches[i].rm_eo - matches[i].rm_so;
printf("group1 [%*.*s]\n", len, len, sz2);
或者你可以使用:
printf("group1 [%*.*s]\n", len, len, buff);
僅列印資料的相關部分。并注意nwellnhof在他們的回答中的評論——這就是我將變數添加i到組合中的原因:您可以迭代匹配項。
這段代碼對我有用:
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MATCHES 10
int main(void)
{
regex_t regex;
int reti;
char msgbuf[100];
char *sz1 = "hello world test";
char *pattern1 = "world.{3}";
regmatch_t matches[MAX_MATCHES];
/* Compile regular expression */
reti = regcomp(®ex, pattern1, REG_EXTENDED);
if (reti)
{
fprintf(stderr, "could not compile\n");
exit(EXIT_FAILURE);
}
printf("Nsubs = %zu\n", regex.re_nsub);
reti = regexec(®ex, sz1, MAX_MATCHES, matches, 0);
if (!reti)
{
for (size_t i = 0; i <= regex.re_nsub; i )
{
char buff[20] = "";
printf("szso[%zu]=%lld\n", i, matches[i].rm_so);
printf("szeo[%zu]=%lld\n", i, matches[i].rm_eo);
memcpy(buff, sz1 matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
printf("group[%zu]: [%s]\n", i, buff);
}
}
else if (reti == REG_NOMATCH)
{
puts("No match");
}
else
{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(EXIT_FAILURE);
}
regfree(®ex);
return(EXIT_SUCCESS);
}
它產生:
Nsubs = 0
szso[0]=6
szeo[0]=14
group[0]: [world te]
因為正則運算式中沒有分組括號,所以沒有子組——你得到的唯一資訊是關于整個匹配文本的資訊。
如果您pattern2將問題用作正則運算式,并使用兩個帶括號的子運算式,則matches當資料匹配時,您將在陣列中獲得更多資料。
通過在每次迭代中使用空值對整個buff陣列進行轉換,復制不會在此樣本資料上以空值終止是沒有問題的。您最好確保它不會在實際代碼中溢位。
如果正則運算式的兩個部分中只有一個匹配(在pattern2問題中),您將收到一個錯誤,regexec()并且可能不依賴于matches陣列中的任何內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523640.html
標籤:C
上一篇:如何使用write()系統呼叫將整數/浮點數寫入檔案描述符?
下一篇:c中簡單二維陣列的意外輸出
