[英]Regular expression <(.*?)> returning < or >
I am attempting to run a regular expression to pull a string of any characters from a file that is contained between "<" and ">". 我正在尝试运行正则表达式以从包含在“ <”和“>”之间的文件中提取任何字符的字符串。 The regex that I have come up with is 我想出的正则表达式是
[ <(.*?)>]
However, when I run this regex using fscanf I only get a "<" or ">" as my output for anything contained within the signs. 但是,当我使用fscanf运行此正则表达式时,对于包含在符号中的任何内容,我只会得到“ <”或“>”作为我的输出。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
int next_word(FILE* filename,char word[254])
{
if (fscanf(filename, "%254[<(.*?)>]", word) == 1)
{
printf("%s\n",word);
return 1;
}
else if (fscanf(filename, "%[^a-zA-Z]", word) == 1) { return 1; }
else if (fscanf(filename, "%254[a-zA-Z]", word) == 1) {return 1; }
return 0;
}
int main(int argc, char * argv[])
{
char word[254];
FILE *infile;
infile = fopen(argv[2],"r");
while(1)
{
if(next_word(infile,word) == 0)
{
break;
}
}
}
My input file is as follows: 我的输入文件如下:
<test> this is a line <end>
Which gives the output: 给出输出:
<
>
<
>
but should give 但应该给
<test>
<end>
Because the format string of scanf family are not regex, and I don't think that your regex will work (you can use online regex testing). 因为scanf系列的格式字符串不是正则表达式,并且我认为您的正则表达式不起作用(您可以使用在线正则表达式测试)。
You can try 你可以试试
fscanf(filename, "<%254[^>]>", word) == 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.