C 在字符串中搜索字符串
我有:C 在字符串中搜索字符串,c,string,C,String,我有:“ETSGYU dededeguwto/$/hfuiehfurei” 我想在“之后和”/$$/“之前获得链,然后:“TOTO” intmain(intargc,char*argv[]) { [1000]中的字符; 字符输出[1000]; strcpy(以“ETSGYU dededeguwto/$$/hfuiehfurei”为单位); ... printf(“%s\n”,OUT); } 向您致意请使用strstr() 为了不给你所有的代码,请使用下面的大纲。OP仍然需要确定4个零件 int
“ETSGYU dededeguwto/$/hfuiehfurei”
我想在“
之后和”/$$/“
之前获得链,然后:“TOTO”
intmain(intargc,char*argv[])
{
[1000]中的字符;
字符输出[1000];
strcpy(以“ETSGYU dededeguwto/$$/hfuiehfurei”为单位);
...
printf(“%s\n”,OUT);
}
向您致意请使用strstr()
为了不给你所有的代码,请使用下面的大纲。OP仍然需要确定4个零件
int main(int argc, char* argv[]) {
char IN[1000];
//char OUT[1000];
strcpy(IN, "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");
const char *pattern1 = "<div>";
const char *pattern2 = "/$$/";
char *start = strstr(IN, pattern1);
if (start) {
// Increase `start` by the length of `pattern1`
start += ...;
// Search again, this time for pattern2. From where should searching start?
char *end = ...;
if (end) {
// print from start. Only need to print some of the characters
int length = ...
printf("%.*s\n", ...);
return 0;
}
}
puts("Fail");
return -1;
}
intmain(intargc,char*argv[]){
[1000]中的字符;
//字符输出[1000];
strcpy(以“ETSGYU dededeguwto/$$/hfuiehfurei”为单位);
常量字符*pattern1=“”;
常量字符*pattern2=“/$$/”;
char*start=strstrstr(IN,pattern1);
如果(启动){
//将'start'的长度增加'pattern1'`
开始+=。。。;
//再次搜索,这次搜索模式2。搜索应从何处开始?
字符*结束=。。。;
若(完){
//从开始打印。只需打印部分字符
整数长度=。。。
printf(“%.*s\n”,…);
返回0;
}
}
看跌期权(“失败”);
返回-1;
}
我用的是这样的东西。但它并不特别安全。因此,如果您知道div和/$$/肯定都在字符串中,那么就可以了。为了更安全,请阅读strstr
char IN[1000];
char* OUT;
char* OUT2;
int found, i;
strcpy(IN,"ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");
OUT=strstr(IN,"<div>");
OUT2=strstr(OUT,"/$$/");
OUT2[0] = '\0';
OUT= OUT +5;
printf("%s\n",OUT);
[1000]中的char;
字符*输出;
char*OUT2;
int发现,i;
strcpy(以“ETSGYU dededeguwto/$$/hfuiehfurei”为单位);
OUT=strstr(输入“”);
OUT2=strstr(OUT,“/$$/”;
OUT2[0]='\0';
OUT=OUT+5;
printf(“%s\n”,OUT);
您有很多方法可以做到这一点,包括:
- Regex:使用
,一开始使用起来有点困难,特别是如果您以前从未操作过Regex。你们可以在互联网上找到很多例子李>Regex.h
- :此函数返回指向第一个字符串参数中第一个出现的第二个字符串参数的指针。所以,你可以这样尝试:
char result[32] = { 0 }; char *str = "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei"; // Find the position of the differents markers ("<div>" and "/$$/") char *marker1 = strstr(str, "<div>"); // Check if the pattern was found if (! marker1) { printf("Could not find string \"<div>\"\n"); return; } // Place marker1 to the beginning of the text to copy marker1 += strlen("<div>"); // Find the second marker "/$$/" char *marker2 = strstr(marker1, "/$$/"); // Check if the pattern was found if (! marker2) { printf("Could not find string \"/$$/\"\n"); return; } // Compute the len of the text to copy int len = marker2 - marker1; // Copy the text to the result buffer, but take care of the buffer overflow strncpy(result, marker1, len < 32 ? len : 32);
charresult[32]={0}; char*str=“ETSGYU dededeguwto/$$/hfuiehfurei”; //查找不同标记(“和”/$$/”的位置 char*marker1=strstrstr(str,“”); //检查是否找到了图案 如果(!marker1){ printf(“找不到字符串\”\\n”); 返回; } //将标记1放在要复制的文本的开头 marker1+=strlen(“”); //查找第二个标记“/$$/” char*marker2=strstrstr(marker1,“/$$/”; //检查是否找到了图案 如果(!marker2){ printf(“找不到字符串\“/$/\”\n”); 返回; } //计算要复制的文本的长度 int len=marker2-marker1; //将文本复制到结果缓冲区,但要注意缓冲区溢出 strncpy(结果,标记1,len<32?len:32);
这是一个快速的例子,可以改进,但主要思想就在这里。另外,要注意缓冲区的长度。一个简单的方法可以如下所示
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void )
{
char in[] = "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei";
char *out = NULL;
const char start[] = "<div>";
const char end[] = "/$$/";
char *p = strstr( in, start );
if ( p )
{
p += sizeof( start ) - 1;
char *q = strstr( p, end );
if ( q )
{
size_t n = q - p;
out = malloc( n + 1 );
if ( out )
{
strncpy( out, p, n );
out[n] = '\0';
}
}
}
if ( out )
{
puts( out );
}
free( out );
return 0;
}
strstr可能是一个很好的起点这不是一个免费的代码编写服务。你试过什么,有什么问题?你看了什么?我相信你在搜索诸如“C从字符串中提取字符串”之类的东西时可以找到很多好东西。这可能会有帮助:。我也可以说,这也会编辑str的内容。如果要将“IN”保留为未编辑状态,则需要进行编辑。我想@chux是在引导你走这条路。为什么是神奇的数字
5
?还考虑如果结束模式与开始模式的结束部分匹配会发生什么。此代码不会在“”之后开始搜索“
”,但在“”
”。5只是跳过长度为5个字符的“”而已。如果要在“”之后开始搜索“”“然后,您只需在第二个'strstr'中输入'OUT+5'。我假设特定的模式在这里很重要。这意味着它们只是这两种特定的模式。但是你在第二个strstr中的'OUT+5'是正确的。@chux:你是对的,代码写得很快,我用你的注释改进了它@恰克斯:没错,我从你之前的评论中忘记了这一点,现在应该更好了。非常感谢。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void )
{
char in[] = "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei";
char *out = NULL;
const char start[] = "<div>";
const char end[] = "/$$/";
char *p = strstr( in, start );
if ( p )
{
p += sizeof( start ) - 1;
char *q = strstr( p, end );
if ( q )
{
size_t n = q - p;
out = malloc( n + 1 );
if ( out )
{
strncpy( out, p, n );
out[n] = '\0';
}
}
}
if ( out )
{
puts( out );
}
free( out );
return 0;
}
TOTO