add me241103_cRe
This commit is contained in:
parent
c28a442c18
commit
7cc9aad0a6
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,7 +1,5 @@
|
||||
4_c_re
|
||||
5_vpi
|
||||
sv_lab
|
||||
.vscode/
|
||||
sv_lab
|
||||
|
||||
me241200_zerotierApi/env/
|
||||
|
||||
@ -37,4 +35,6 @@ me241102_vpi/ucli.key
|
||||
me241102_vpi/vc_hdrs.h
|
||||
me241102_vpi/wave.fsdb
|
||||
me241102_vpi/*.o
|
||||
me241102_vpi/*.so
|
||||
me241102_vpi/*.so
|
||||
|
||||
me241103_cRe/main
|
||||
|
@ -9,5 +9,6 @@ script work是一系列自己编选的脚本合集。
|
||||
| [me241100_svExample](./me241100_svExample) | 使用vcs编译sv的简单例子 | 100% | | |
|
||||
| [me241201_dpi](./me241101_dpi) | 使用vcs编译dpi的简单例子 | 100% | [PLI 学习](https://www.cnblogs.com/fengbohan/p/18595069/pli-learning-zx6kpe) | |
|
||||
| [me241102_vpi](./me241102_vpi) | 使用vcs编译vpi的简单例子 | 100% | [PLI 学习](https://www.cnblogs.com/fengbohan/p/18595069/pli-learning-zx6kpe) | |
|
||||
| [me241103_cRe](./me241103_cRe) | 使用c语言进行正则匹配的例子 | 100% | | |
|
||||
| [me241200_zerotierApi](./me241200_zerotierApi) | 使用zerotier的api获取网络信息,生成smartdns的配置文件 | 100% | | |
|
||||
|
||||
|
2
me241103_cRe/Makefile
Normal file
2
me241103_cRe/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
main:
|
||||
gcc -o main main.c
|
22
me241103_cRe/ReadMe.md
Normal file
22
me241103_cRe/ReadMe.md
Normal file
@ -0,0 +1,22 @@
|
||||
# cRE
|
||||
|
||||
## 概述
|
||||
|
||||
使用c语言,进行正则表达式的例子。
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 一、编译
|
||||
|
||||
```bash
|
||||
make
|
||||
```
|
||||
|
||||
### 二、运行
|
||||
|
||||
```bash
|
||||
./main
|
||||
```
|
||||
|
||||
|
||||
|
44
me241103_cRe/main.c
Normal file
44
me241103_cRe/main.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <regex.h>
|
||||
|
||||
#define MAX_MATCHES 10
|
||||
|
||||
void extract_numbers(const char *text) {
|
||||
regex_t regex;
|
||||
regmatch_t matches[MAX_MATCHES];
|
||||
const char *pattern = "[0-9]+";
|
||||
int ret;
|
||||
|
||||
ret = regcomp(®ex, pattern, REG_EXTENDED);
|
||||
if (ret != 0) {
|
||||
char error_message[100];
|
||||
regerror(ret, ®ex, error_message, sizeof(error_message));
|
||||
printf("Regex compilation error: %s\n", error_message);
|
||||
return;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ret = regexec(®ex, text, MAX_MATCHES, matches, 0);
|
||||
if (ret != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < MAX_MATCHES && matches[i].rm_so != -1; i++) {
|
||||
int start = matches[i].rm_so;
|
||||
int end = matches[i].rm_eo;
|
||||
printf("Match found: %.*s\n", end - start, text + start);
|
||||
}
|
||||
|
||||
printf("%s\n", text);
|
||||
text += matches[0].rm_eo;
|
||||
}
|
||||
|
||||
regfree(®ex);
|
||||
}
|
||||
|
||||
int main() {
|
||||
const char *text = "abc123xyz456";
|
||||
extract_numbers(text);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user