简体   繁体   English

供应代码正则表达式

[英]Provisioning code regular expression

I'm trying to develop a C code that checks the validity of "Provisioning code" string using regular expressions. 我正在尝试开发一个C代码,使用正则表达式检查“ Provisioning code”字符串的有效性。 A "provisioning code" format should respect the following law: “配置代码”格式应遵守以下法律:

If not an empty string, this argument SHOULD be in the form of a hierarchical descriptor with one or more nodes specified. 如果不是空字符串,则此参数应采用分层描述符的形式,并指定一个或多个节点。 Each node in the hierarchy is represented as a 4-character sub-string, containing only numerals or upper-case letters. 层次结构中的每个节点都表示为4个字符的子字符串,仅包含数字或大写字母。 If there is more than one node indicated, each node is separated by a "." 如果指示的节点不止一个,则每个节点都用“。”分隔。 (dot). (点)。 Examples: "TLCO" or "TLCO.GRP2". 例如:“ TLCO”或“ TLCO.GRP2”。

I started development using the code in this link http://web.archive.org/web/20160308115653/http://peope.net/old/regex.html 我使用此链接http://web.archive.org/web/20160308115653/http://peope.net/old/regex.html中的代码开始开发

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>

int main(int argc, char *argv[]){
    regex_t regex;
    int reti;
    char msgbuf[100];

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

/* Execute regular expression */
    reti = regexec(&regex, "abc", 0, NULL, 0);
    if( !reti ){
            puts("Match");
    }
    else if( reti == REG_NOMATCH ){
            puts("No match");
    }
    else{
            regerror(reti, &regex, msgbuf, sizeof(msgbuf));
            fprintf(stderr, "Regex match failed: %s\n", msgbuf);
            exit(1);
    }

/* Free compiled regular expression if you want to use the regex_t again */
    regfree(&regex);

    return 0;
}

this code works fine but my problem is what's the best regular expression that should be input of the function regcomp. 这段代码可以正常工作,但我的问题是应该输入regcomp函数的最佳正则表达式是什么。

I started to try with a regular expression that matches with string that contains exactly 4 characters uppercases or numerals that means example like TLCO or TLC2 trying with the regular expression "[A-Z0-9]{4}" but I get "No match" as output with matches examples like TLC2. 我开始尝试使用正则表达式,该正则表达式与正好包含4个字符的大写字母或数字的字符串匹配,这意味着像TLCO或TLC2这样的示例尝试使用正则表达式“ [A-Z0-9] {4}”,但得到“没有匹配项”作为与匹配示例(例如TLC2)的输出。

Is there a suggestion about the right regular expression that should be input of regcomp and matches with "provisioning code"? 是否有关于应该输入regcomp并与“ provisioning code”匹配的正确正则表达式的建议?

You may use the following regex that will work alright if you also pass REG_EXTENDED flag to the regcomp method (for the $ and {m,n} modifier to work correctly): 如果您还向regcomp方法传递REG_EXTENDED标志(为了使${m,n}修饰符正常工作) {m,n}则可以使用下面的正则表达式,它将正常运行:

^[A-Z0-9]{4}([.][A-Z0-9]{4})*$

C code: C代码:

reti = regcomp(&regex, "^[A-Z0-9]{4}([.][A-Z0-9]{4})*$", REG_EXTENDED);

Details 细节

  • ^ - start of string ^ -字符串开头
  • [A-Z0-9]{4} - 4 uppercase ASCII letters or digits [A-Z0-9]{4} -4个大写ASCII字母或数字
  • ([.][A-Z0-9]{4})* - zero or more sequences of: ([.][A-Z0-9]{4})* -零个或多个序列:
    • [.] - a literal . [.] -文字. char 烧焦
    • [A-Z0-9]{4} - 4 uppercase ASCII letters or digits [A-Z0-9]{4} -4个大写ASCII字母或数字
  • $ - end of string. $ -字符串结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM