使用 GNU C 正则表达式库的字符串正则表达式
我正在编写一个与 GNU C 正则表达式库一起使用的正则表达式:
字符串的形式为:(斜体文本是内容的描述)
(不是#)开始(可能是空格):数据
我已经编写了以下代码,但它不匹配。
regcomp(&start_state, "^[^#][ \\t]*\\(start\\)[ \\t]*[:].*$", REG_EXTENDED);
我需要写什么?
例子: 匹配:
状态:q0
状态:q0
状态:q0s
不匹配:
#状态:q0
状态 q0
# 状态:q0
谢谢!
I am writing a regex to use with the GNU C regex library:
The string is of the form: (text in italics is a description of content)
(NOT a #) start (maybe whitespace) : data
I have written the following code, but it won't match.
regcomp(&start_state, "^[^#][ \\t]*\\(start\\)[ \\t]*[:].*$", REG_EXTENDED);
What do I need to write?
examples:
to match:
state : q0
state: q0
state:q0s
not to match:
#state :q0
state q0
# state :q0
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我想通了:
上面解决了我的问题! (事实证明,我忘了在 [^#] 之后加一个 *)...
无论如何,感谢您的帮助,鲁本斯! :)
Ok, I figured it out:
above solves my problem! (turns out, I forgot to put a * after [^#])...
Thanks for your help anyway, Rubens! :)
这适用于您的示例数据:
编辑:我不知道,但您需要启用多行支持,如第一个
^
和最后一个$ 与该设置有不同的行为。
This works with your sample data:
EDIT: I don't know, but you'll need to enable multiline support, as first
^
and last$
have a different behavior with that setting.您问题中的模式将
state
中的第一个字母与[^#]
一起使用,这导致匹配无法继续,因为它尝试匹配tate 与模式
\(state\)
相对应。您传递了标志
REG_EXTENDED
,这意味着您不会转义捕获括号,但会转义文字括号。使用正则表达式,说出您想要匹配的内容:
如
输出所示:
The pattern in your question was consuming the first letter in
state
with[^#]
, which left the match unable to proceed because it tries to matchtate
against the pattern\(state\)
.You passed the flag
REG_EXTENDED
which means you don't escape capturing parentheses but do escape literal parentheses.With regular expressions, say what you do want to match:
as in
Output: