簡體   English   中英

C中的正則表達式用於匹配

[英]Regex in C For Matching

我需要制作一個正則表達式,以匹配長度<99的任何字母數字字符串,並用兩個@括起來。 “ @”之后的第一個字符也可以是“ _”,我不確定該怎么解釋。 防爆。 @ U001 @是有效的。 @ _A111 @也將有效。 但是,@ ____ ABC @無效,@ ABC也無效。

我對regex比較陌生,並且注意到\\ z是無法識別的轉義序列。 如果這很重要,我想在C11中編寫它。

#include <regex.h>        
regex_t regex;
int reti;
char msgbuf[100];

/* Compile regular expression */
reti = regcomp(&regex, "^@[[:alnum:]]@\z", 0);
if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
}

嘗試使用以下模式:

^@[_[:alnum:]][:alnum:]{0,97}@

這是模式的簡要說明

^                from the start of the string
@                match @
[_[:alnum:]]     match underscore or alpha
[:alnum:]{0,97}  then match zero to 97 alpha
@                match @

碼:

reti = regcomp(&regex, "^@[_[:alnum:]][:alnum:]{0,97}@", 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM