[英]posix regular expression for parsing uevent causing error
我正在嘗試使用下面的代碼解析uevent,但我認為我的正則表達式不合適,導致regcomp函數失敗。
有人可以幫忙嗎? 我試圖做類似這樣 。
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main ()
{
char * source = "change@/devices/soc/799999.i2c/i2c-3/3-0015/power_supply/battery";
char * regexString = "(?<action>[a-zA-Z]+)@\\/(?<dev_path>.*)\\/(?<subsystem>[a-zA-z]+)\\/(?<name>[a-zA-z]+)";
size_t maxGroups = 4;
regex_t regexCompiled;
regmatch_t groupArray[maxGroups];
if (regcomp(®exCompiled, regexString, REG_EXTENDED))
{
printf("Could not compile regular expression.\n");
return 1;
};
regfree(®exCompiled);
return 0;
}
我收到“無法編譯正則表達式。”。 這意味着regcomp無法識別regex。
當我使用代碼報告錯誤時:
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main(void)
{
//char * source = "change@/devices/soc/799999.i2c/i2c-3/3-0015/power_supply/battery";
char * regexString = "(?<action>[a-zA-Z]+)@\\/(?<dev_path>.*)\\/(?<subsystem>[a-zA-z]+)\\/(?<name>[a-zA-z]+)";
//size_t maxGroups = 4;
regex_t regexCompiled;
//regmatch_t groupArray[maxGroups];
int rc;
if ((rc = regcomp(®exCompiled, regexString, REG_EXTENDED)) != 0)
{
char buffer[1024];
regerror(rc, ®exCompiled, buffer, sizeof(buffer));
printf("Could not compile regular expression (%d: %s).\n", rc, buffer);
return 1;
}
regfree(®exCompiled);
return 0;
}
我得到的輸出:
Could not compile regular expression (13: repetition-operator operand invalid).
問題出在符號(?
您正在使用:
"(?<action>[a-zA-Z]+)@\\/(?<dev_path>.*)\\/(?<subsystem>[a-zA-z]+)\\/(?<name>[a-zA-z]+)"
該表示法僅適用於PCRE ,而不適用於POSIX。 和PCRE一起使用?
之后(
正是因為它在其他正則表達式系統(例如POSIX)中無效。
因此,如果要使用PCRE正則表達式,請安裝並使用PCRE庫。
否則,您將需要使用:
"([a-zA-Z]+)@\\/(.*)\\/([a-zA-z]+)\\/([a-zA-z]+)"
設置好之后,並注意到您需要一個regmatch_t
來匹配整個字符串,並加上4個捕獲的組(總共5個捕獲),您可以編寫:
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main(void)
{
char *source = "change@/devices/soc/799999.i2c/i2c-3/3-0015/power_supply/battery";
// char * regexString = "(?<action>[a-zA-Z]+)@\\/(?<dev_path>.*)\\/(?<subsystem>[a-zA-z]+)\\/(?<name>[a-zA-z]+)";
size_t maxGroups = 5;
char *regexString = "([a-zA-Z]+)@\\/(.*)\\/([a-zA-z]+)\\/([a-zA-z]+)";
regex_t regexCompiled;
regmatch_t groupArray[maxGroups];
int rc;
if ((rc = regcomp(®exCompiled, regexString, REG_EXTENDED)) != 0)
{
char buffer[1024];
regerror(rc, ®exCompiled, buffer, sizeof(buffer));
printf("Could not compile regular expression (%d: %s).\n", rc, buffer);
return 1;
}
if ((rc = regexec(®exCompiled, source, maxGroups, groupArray, 0)) != 0)
{
char buffer[1024];
regerror(rc, ®exCompiled, buffer, sizeof(buffer));
printf("Could not execute regular expression (%d: %s).\n", rc, buffer);
return 1;
}
printf("Match successful:\n");
for (size_t i = 0; i < maxGroups; i++)
{
int so = groupArray[i].rm_so;
int eo = groupArray[i].rm_eo;
printf("%zu: %d..%d [%.*s]\n", i, so, eo, eo - so, &source[so]);
}
regfree(®exCompiled);
return 0;
}
輸出為:
Match successful:
0: 0..64 [change@/devices/soc/799999.i2c/i2c-3/3-0015/power_supply/battery]
1: 0..6 [change]
2: 8..43 [devices/soc/799999.i2c/i2c-3/3-0015]
3: 44..56 [power_supply]
4: 57..64 [battery]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.