gpt4 book ai didi

c - 如何制作受正则表达式启发的字符串函数?

转载 作者:行者123 更新时间:2023-11-30 15:27:18 25 4
gpt4 key购买 nike

问题:

看看我的“扫描”功能。这就是决定匹配项在字符串中的位置的因素。 (顺便说一句,这是 EEL 代码,它是一种非常类似于 C 的语言)。我如何开始添加类似正则表达式的匹配?我的意图是受正则表达式启发的函数,因为我不想或不需要精确复制正则表达式行为。具体来说,我想窃取正则表达式的\d 功能,基本上是一个字符类,意味着“匹配从‘0’到‘9’的任何字符。

示例:“abc\d\d​​\d”匹配“abc 123”;

任何关于开始添加此类功能的解释和有用的提示都是值得赞赏的!

详细信息:

这是我的“双向字符串扫描器”函数,它输出子字符串。它是双向的,因为你必须指定两个匹配 m1 m2,两个“在停止之前你想匹配多少次?” (称为ntimes1 ntimes2)起始位置p1,当然还有要扫描的字符串(干草堆)。

它可以向前移动“ff”向后向后 'bb'向后 向前 'bf'并向后转发“fb”。

例如,假设您想要第二次出现“abc”之前的数字:

“abc,1,def,4,ghi,5,abc,2,def,6,ghi,3”

从 0 开始,向前移动并匹配“abc”两次,向后移动:匹配“,”两次

substring = ",5,abc"(当前子字符串包含所有匹配字符串)

效果很好!

代码:

function scan(match,str,p,D,ntimes,mlen,hlen) local(lastp found break restart) (
D == -1 ? (m=restart=mlen-1; end=-1) : (m=restart=0; end=mlen);
found=break=0;
while(p > -1 && p < hlen && !break) (
//ShowConsoleMsg(sprintf(#, "%i:%i -> ' %c ' = ' %c '\n", m,p,str_getchar(match, m),str_getchar(str, p)));
str_getchar(match, m) == str_getchar(str, p) ? m+=D : m=restart;
m == end ? (found+=1; m=restart; lastp=p);
found == ntimes ? break=1 : p+=D;
);
ntimes == -1 ? lastp : p;
);

function findpos(m1,m2,DIR,p1,ntimes1,ntimes2) local(adj start p1 p2 len1 len2 dir1 dir2 hlen) (
hlen = strlen(this);
len1 = strlen(m1);
len2 = strlen(m2);
ntimes1 < 1 ? ntimes1 = -1;
ntimes2 < 1 ? ntimes2 = -1;

DIR == 'ff' ? (dir1 = dir2 = +1; adj=1):
DIR == 'fb' ? (dir1 = +1; dir2 = -1; adj=1):
DIR == 'bf' ? (dir1 = -1; dir2 = +1; adj=1):
(dir1 = dir2 = -1; adj=len1);

p1 = scan(m1,this,p1,dir1,ntimes1,len1,hlen);

DIR == 'ff' ? (p2=p1+len1+dir1; p1+=dir1-len1):
DIR == 'bf' ? (p2=p1+len1):
(p2=p1-len1);
//ShowConsoleMsg(sprintf(#, "%i---------%i\n", p1,p2));
p2 = scan(m2,this,p2,dir2,ntimes2,len2,hlen);

dir2 == 1 ? (p2+=len; this.pos1 = p1; this.pos2 = p2):
DIR == 'fb' ? (p2+=len; this.pos1 = p2; this.pos2 = p1):
(this.pos1 = p2; this.pos2 = p1);
//ShowConsoleMsg(sprintf(#, "%i---------%i\n", p1,p2));
this.substrlen = this.pos2-this.pos1+adj;
strcpy_substr(#, this, this.pos1, this.substrlen);
);

使用示例:

ShowConsoleMsg("");
string = "C-2=0,C#-2=1,D-2=2,D#-2=3,";

ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",",'ff',8,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",",'fb',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",",'bf',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",",'bb',21,1,1)) );

ShowConsoleMsg( sprintf(#, "%s\n",string.findpos("1,",",D",'ff',8,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",D","1,",'fb',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos("1,",",D",'bf',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",D","1,",'bb',21,1,1)) );

ShowConsoleMsg( sprintf(#, "%s\n",string.findpos("1,",",",'ff',8,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",","1,",'fb',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos("1,",",",'bf',15,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",","1,",'bb',21,1,1)) );

ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",D",'ff',8,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",D",",",'bb',21,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",",",D",'bf',16,1,1)) );
ShowConsoleMsg( sprintf(#, "%s\n",string.findpos(",D",",",'fb',15,1,1)) );

输出:

,D-2=2,
,D-2=2,
,D-2=2,
,D-2=2,
1,D-2=2,D
1,D-2=2,D
1,D-2=2,D
1,D-2=2,D
1,D-2=2,
1,D-2=2,
1,D-2=2,
1,D-2=2,
,D-2=2,D
,D-2=2,D
,D-2=2,D
,D-2=2,D

最佳答案

我实际上并没有运行你的代码,但快速浏览了一下,我认为你想更改这一行:

str_getchar(match, m) == str_getchar(str, p) ? m+=D : m=restart; 

现在,它会寻找字符的完全匹配。您可以更改它,以便如果搜索字符串是魔术字符类字符,则它不会检查确切的内容,而是检查范围。

很喜欢

char c = str_getchar(match, m);
if(c == magic_thing) {
char n = str_getchar(str, p);
(n >= '0' && n <= '9') ? m+=D : m=restart;
else
c == str_getchar(str, p) ? m +=D : m=restart;
}

在那里。要执行 magic_thing 操作,您可以修改 str_getchar (或为其编写一个包装器)来检查 \。如果存在,也读取下一个字符。如果是“d”,则返回魔法能指。否则,返回字符本身。您可能会采取一些不同的做法,但通常我希望将\d 识别放入读取字符串函数中,而不是尝试将其作为扫描函数本身的一部分。

关于c - 如何制作受正则表达式启发的字符串函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27072653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com