linux c语言从字符串“here:GET /api/shortcut?enable=1 HTTP/1.1”取出“enable=”后面的数字
时间: 2023-12-08 08:06:31 浏览: 35
可以使用C语言中的字符串处理函数来实现这个功能。具体的做法是:
1. 使用strstr函数找到字符串中"enable="的位置。
2. 使用sscanf函数从该位置开始读取数字。
下面是代码示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "here:GET /api/shortcut?enable=1 HTTP/1.1";
char* pos = strstr(str, "enable=");
if (pos == NULL) {
printf("Not found.\n");
} else {
int num;
sscanf(pos + 7, "%d", &num);
printf("The number is: %d\n", num);
}
return 0;
}
```
输出结果为:
```
The number is: 1
```
其中,`pos + 7`表示从"enable="后面的第一个字符开始读取数字。`%d`表示读取整数。
相关问题
linux c语言使用正则表达式从字符串“here:GET /api/shortcut?enable=1 HTTP/1.1”取出“enable=”后面的数字
可以使用regex库来实现正则表达式的匹配和提取。以下是一个示例代码:
```c
#include <stdio.h>
#include <regex.h>
int main(void) {
char *str = "here:GET /api/shortcut?enable=1 HTTP/1.1";
regex_t regex;
regmatch_t match[2];
// 编译正则表达式
int ret = regcomp(®ex, "enable=([0-9]+)", REG_EXTENDED);
if (ret != 0) {
printf("regcomp error\n");
return 1;
}
// 匹配字符串
ret = regexec(®ex, str, 2, match, 0);
if (ret == 0) {
// 提取数字
char enable[match[1].rm_eo - match[1].rm_so + 1];
snprintf(enable, sizeof(enable), "%.*s", match[1].rm_eo - match[1].rm_so, str + match[1].rm_so);
printf("enable=%s\n", enable);
} else if (ret == REG_NOMATCH) {
printf("no match\n");
} else {
printf("regexec error\n");
return 1;
}
// 释放正则表达式
regfree(®ex);
return 0;
}
```
运行结果为:
```
enable=1
```
正则表达式"enable=([0-9]+)"中,"[0-9]+"表示匹配一个或多个数字,"()"表示将这个部分作为一个子匹配,方便后面提取数字。执行regexec函数后,如果匹配成功,可以根据match数组中的rm_so和rm_eo字段来提取子匹配的内容。
http://localhost:8081/favicon.ico
根据引用和引用的内容,你可以在页面的<head>标签中使用以下代码来引用http://localhost:8081/favicon.ico:
```html
<link rel="shortcut icon" href="http://localhost:8081/favicon.ico" type="image/x-icon">
```
这样就可以在你的网站上显示指定的favicon图标了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [http://localhost:8080/favicon.ico 404](https://blog.csdn.net/m0_59776581/article/details/118409809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [GET http://localhost:8080/favicon.ico 404 报错解决](https://blog.csdn.net/Cinyyyy/article/details/123683572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![crx](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)