如何检查字符串是否包含C中的有效字符?

时间:2013-07-15 22:49:51

标签: c arrays parsing error-handling character

所以我们有一个令牌器的代码,这个代码确实有点破碎。

现在我们无法修改此功能。

尽管如此,它为大多数情况提供了正确的输出。除了在某些情况下,它会添加随机的额外垃圾。

现在,程序读入一个字符串并将其转换为树以供执行。最底层的节点包含简单的命令。

必须解析这些简单的命令才能执行。

现在在解析过程中发现最后的额外垃圾阻碍了execvp()调用。

我想询问有关如何在最后消除垃圾的建议。

我想过使用正则表达式来检查字符串是否包含有效字符,并且好奇是否有任何标准库函数可以帮助解决这个问题?

如果它有任何区别 - 垃圾字符就像一个带

的方框
00
19

作为其内容。它显示在gedit中但不在此处。垃圾打印的起源是第一个解析,更改它是不可行的。

以下是该程序的示例运行。

sgml@SGML3:~/smelly-fish$ ./timetrash exectest
 Word is: expr 3 + 8 + 9 + 8  (Junk - this character won't display)
 Temp is: expr 3 + 8 + 9 + 8  (Junk - this character won't display)
c->u.word[0] is expr
c->u.word[1] is 3
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is  (Junk - this character won't display)
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
 Word is:  expr 7 + 8 + 9 + 8  (Junk - this character won't display)
 Temp is:  expr 7 + 8 + 9 + 8 (Junk - this character won't display)
c->u.word[0] is expr
c->u.word[1] is 7
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is (Junk - this character won't display)
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
sgml@SGML3:~/smelly-fish$ ./timetrash exectest >execresults1.txt
expr: syntax error
expr: syntax error
sgml@SGML3:~/smelly-fish$ ./timetrash exectest
 Word is: expr 3 + 8 + 9 + 8 (Junk - this character won't display)  
 Temp is: expr 3 + 8 + 9 + 8    (Junk - this character won't display) 
c->u.word[0] is expr
c->u.word[1] is 3
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is (Junk - this character won't display)   
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
 Word is:  expr 7 + 8 + 9 + 8 
 Temp is:  expr 7 + 8 + 9 + 8 
c->u.word[0] is expr
c->u.word[1] is 7
c->u.word[2] is +
c->u.word[3] is 8
c->u.word[4] is +
c->u.word[5] is 9
c->u.word[6] is +
c->u.word[7] is 8
c->u.word[8] is 
c->u.word[9] is (null)
expr: syntax error
Command is: SIMPLE_COMMAND
sgml@SGML3:~/smelly-fish$ 

0 个答案:

没有答案