要提取文本中的數(shù)據(jù),可以使用C語言中的字符串處理函數(shù)和正則表達(dá)式庫。以下是一種提取數(shù)字的示例代碼:
#include <stdio.h>
#include <string.h>
#include <regex.h>
int main() {
char text[] = "The price of the product is $99.99";
char pattern[] = "\\$([0-9]+\\.[0-9]+)";
regex_t regex;
regmatch_t matches[2];
if(regcomp(®ex, pattern, REG_EXTENDED) != 0) {
printf("Error compiling regex\n");
return 1;
}
if(regexec(®ex, text, 2, matches, 0) == 0) {
char price[20];
strncpy(price, text + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
price[matches[1].rm_eo - matches[1].rm_so] = '\0';
printf("Price: %s\n", price);
} else {
printf("No match found\n");
}
regfree(®ex);
return 0;
}
在這個示例中,我們使用正則表達(dá)式來匹配文本中的價格($99.99)。我們首先編譯正則表達(dá)式,然后使用regexec函數(shù)在文本中查找匹配項(xiàng)。如果找到匹配項(xiàng),我們從文本中提取價格并打印出來。最后,我們釋放正則表達(dá)式對象。
請注意,這只是一個簡單的示例,實(shí)際的文本數(shù)據(jù)提取可能需要更復(fù)雜的正則表達(dá)式和處理邏輯。