1998年,Brian在编写“编程实践”一书时,想演示正则表达式引擎的实现。但是很难找到小引擎,这种鳕鱼可以在书中找到。
于是布赖恩·克尼汉请罗布·派克为他写一封信,罗布·做到了吗?优雅地写了一封信。这是原始资料来源:
/* match: search for regexp anywhere in text */
int match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do { /* must look even if string is empty */
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
/* matchhere: search for regexp at beginning of text */
int matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '规则编写支持相同操作的regex引擎,如Rob的:任何字符(作为符号)匹配任何字符的.匹配字符串开头的^匹配字符串结束的$*以匹配从零开始的任何字符计数。辅助代码不计数(例如接受输入)做出最短的解决方案,击败罗布派克!输入两个字符串:一个用于正则表达式,一个用于匹配。输出True (在您的语言中),如果regex与字符串匹配False (在您的语言中),如果regex不匹配字符串 && regexp[1] == '\0')
return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
return matchhere(regexp+1, text+1);
return 0;
}
/* matchstar: search for c*regexp at beginning of text */
int matchstar(int c, char *regexp, char *text)
{
do { /* a * matches zero or more instances */
if (matchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}K12规则K23F14H15编写支持相同操作的regex引擎,如Rob的:F16H17
任何字符(作为符号)
H28H19
匹配任何字符的D10
H211H112
匹配字符串开头的D13
H214H115
匹配字符串结束的D16
H217H118
D19以匹配从零开始的任何字符计数。
H220F221H222F223F124H125
辅助代码不计数(例如接受输入)
H226H127
做出最短的解决方案,击败罗布派克!
H228F229K130输入K231
两个字符串:一个用于正则表达式,一个用于匹配。
K132输出K233F134H135
D36 (在您的语言中),如果regex与字符串匹配
H237H138
D39 (在您的语言中),如果regex不匹配字符串
H240F241
https://codegolf.stackexchange.com/questions/189674
复制相似问题