我正在使用fget读取一个文件。我需要根据regex检查文件的每一行。如果有非alpha数字字符,则需要退出程序,并显示行号和“坏”字符。正在发生的事情是,它在“坏”的角色面前被踢走了。这是我的.dat文件:
howard jim dave
joe
(
Maggie我的程序输出是:
file opened
Digit: howard jim dave
is not alphanumeric on line: 1
Exiting program!
File closed应该发生的是,它应该被踢出第3行,因为你可以看到这是没有发生的。
下面是我的regex,它在我的main.h文件中:
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
#define BUFF 1024
#define to_find "^[a-zA-Z0-9]+$"这是我的文件
#include "main.h"
int fileCheck(FILE *fp)
{
int ret_val;
int line_count = 0;
char file[BUFF];
regex_t regex;
if (regcomp(®ex, to_find, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
if (fp != NULL)
{
while (fgets(file, BUFF, fp))
{
line_count++;
if ((ret_val = regexec(®ex, file, 0, NULL, 0)) != 0)
{
printf("Digit: %s is not alphanumeric on line: %d\n", file, line_count);
printf("Exiting program!\n");
return EXIT_FAILURE;
}
}
}
}我不确定"\n“字是否是问题所在。我不认为是这样。我很清楚isalnum(),但我的任务是一个正则表达式。解决这一问题的可能办法是什么?谢谢你的建议。
编辑:我想提一下,当我使用fscanf而不是fget时,上面的regex工作得很好。更改的原因是我需要计算每一行。如果我是正确的,fscanf会忽略换行符。我需要一些方法来数新行。是否有可能计算一个新的使用fscanf?我最初的文件读取循环是:
while (fscanf(fp, "%11023s", file) != EOF
{
line_count++;
if (regexec(®ex, file, 0, NULL, 0) != 0)
{
printf("%s%d wrong:\n, file, line_count);
return EXIT_FAILURE;
}
}发布于 2013-09-15 17:43:36
howard jim dave包含空白空间。
Edit3:
我把注意力集中在只寻找有效行的匹配上的原因是,你似乎
正在使用一个简单的测试场景,稍后将更加复杂。
然而,如果这正是您所需要的,那么真正的解决方案就是去寻找
非字母数字的非空白字符。
如果您所使用的regex风格要求从头到尾匹配,
这不管用。
#define to_find "[^a-zA-Z0-9\\s]"
or,
#define to_find "[^a-zA-Z0-9\\ \\t\\f\\r\\n]"
. . .
Then down here if the regex matches, it found non alpha numeric
if ( regexec(®ex, file, 0, NULL, 0)) == 0 )
{
printf("Digit: %s is not alphanumeric on line: %d\n", file, line_count);
printf("Exiting program!\n");
return EXIT_FAILURE;
}Edit2:
这是Posix引擎吗?regcomp()返回哪些错误代码?您应该将REG_EXTENDED设置为一个标志参数。
不幸的是,(?: pattern )结构是一个扩展的规范。
不如把厨房的水槽扔到里面去
REG_EXTENDED | REG_NEWLINE
试试看那些亚麻和汽水
直接进入regcomp()的"^\\s*[a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]+)*\\s*$"
这可以帮助处理错误代码:
int res_compile = 0;
if ( (res_compile=regcomp(®ex, to_find, REG_EXTENDED) ) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\nError code: %d\n", to_find, res_compile);
}也许你需要
# ^\s*[a-zA-Z0-9]+(?:\s+[a-zA-Z0-9]+)*\s*$
^
\s*
[a-zA-Z0-9]+
(?: \s+ [a-zA-Z0-9]+ )*
\s*
$或
# \A[^\S\r\n]*[a-zA-Z0-9]+(?:[^\S\r\n]+[a-zA-Z0-9]+)*\s*\z
\A
[^\S\r\n]*
[a-zA-Z0-9]+
(?: [^\S\r\n]+ [a-zA-Z0-9]+ )*
\s*
\zhttps://stackoverflow.com/questions/18815237
复制相似问题