Python中文
首页
教程
问答
标签
搜索
登录
注册
为什么Python“分组”不适用于C语言中的正则表达式?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>这是我的Python程序:</p> <pre><code>import re print re.findall( "([se]{2,30})ting", "testingtested" ) </code></pre> <p>其输出为:</p> ^{pr2}$ <p>这正是我所期待的。我希望返回“es”,因为我搜索了2-30个字符“e”或“s”,后面跟着“ting”。在</p> <p>这是我的C程序:</p> <pre><code>#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <regex.h> int main(void) { regex_t preg; regmatch_t pmatch; char string[] = "testingtested"; //Compile the regular expression if ( regcomp( &preg, "([se]{2,30})ting", REG_EXTENDED ) ) { printf( "ERROR!\n" ); return -1; } else { printf( "Compiled\n" ); } //Do the search if ( regexec( &preg, string, 1, &pmatch, REG_NOTEOL ) ) { printf( "No Match\n" ); } else { //Allocate memory on the stack for this char substring[pmatch.rm_eo - pmatch.rm_so + 1]; //Copy the substring over printf( "%d %d\n", pmatch.rm_so, pmatch.rm_eo ); strncpy( substring, &string[pmatch.rm_so], pmatch.rm_eo - pmatch.rm_so ); //Make sure there's a null byte substring[pmatch.rm_eo - pmatch.rm_so] = 0; //Print it out printf( "Match\n" ); printf( "\"%s\"\n", substring ); } //Release the regular expression regfree( &preg ); return EXIT_SUCCESS; } </code></pre> <p>它的输出是:</p> <pre><code>Compiled 1 7 Match "esting" </code></pre> <p>为什么C程序会在结果中包含“ting”?我有没有办法排除“婷”的部分?在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>虽然正则表达式“在任何地方都或多或少相同”,但具体支持的功能因实现而异。在</p> <p>不幸的是,在设计正则表达式时,需要分别查阅每个regex库的文档。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何重塑数组、迭代列的所有行并将重塑后的数组分配给新列?Python/Pandas/Numpy
5 回答
如何重塑数组的形状?
8 回答
如何重塑文本数据以适应keras的LSTM模型
5 回答
如何重塑未对齐的数据集,并使用numpy丢弃剩余数据?
7 回答
如何重塑此数据以使用绘图
2 回答
如何重塑此数据帧?
9 回答
如何重塑此数据集以适应RNN
7 回答
如何重塑没有列的数组?
5 回答
如何重塑测试数据帧,使其维数与训练和预测工作中使用的维数相同?
1 回答
如何重塑系列以在StandardScaler中使用它
8 回答
如何重塑线性回归的数据
5 回答
如何重塑线性回归的数据?
5 回答
如何重塑表格?
7 回答
如何重塑要堆叠的重复宽数据帧?
2 回答
如何重塑输入以放入二维层?
7 回答
如何重塑输入神经网络的三通道数据集
6 回答
如何重塑这个numpy数组
5 回答
如何重塑这个numpy数组以排除“额外维度”?
6 回答
如何重塑这个numpy阵列?
4 回答
如何重塑这个数据帧
7 回答