使用regex.h时如何匹配c中的字母“$”?

时间:2013-05-07 06:41:00

标签: c regex

有一个字符串:

  

“fdsfsfsfsfsdomnol $ natureOrder(0123)jqnm”

我想匹配子字符串:$ natureOrder(0123),我这样做:

regcomp(&reg, "\$natureOrder\([0-9]{1,4}\)", cflags);

但它不起作用!如何编写正则表达式模式?

1 个答案:

答案 0 :(得分:4)

除了$之外,您需要在正则表达式中使用括号,并且必须对这些括号进行转义。

所以正则表达式是

\$natureOrder\([0-9]{1,4}\)

在C字符串中,因为\是转义序列的开始:

regcomp(&reg, "\\$natureOrder\\([0-9]{1,4}\\)", cflags);