python - 如何提取数字之间的字符串? (并保留字符串中的第一个数字?)

标签 python regex text text-mining changelog

我正在尝试使用正则表达式从更改日志中提取数据。以下是更改日志的结构示例:

96545
this is some changes in the ticket
some new version: x.x.22
another change
new version: x.y.2.2
120091
this is some changes in the ticket
some new version: z.z.22
another change
another change
another change
new version: z.y.2.2
120092
...
...
...
  • 每个数据点都以 ID 开头,ID 范围为 5 到 6 位数字。
  • 此外,每个 ID 的日志中的更改量(行)都是可变的。
  • 每个数据点均以新版本:*** 结尾。 *** 是每个 ID 都可变的字符串。

我正在使用 RegExStrom Tester测试我的正则表达式。

到目前为止,我有: ^\w{5,6}(.|\n)*?\d{5,6} 但是结果包括下一张票的 ID,其中我需要避免。

结果:

96545
this is some changes in the ticket
some new version: x.x.22
another change
new version: x.y.2.2
120091 

预期结果:

96545
this is some changes in the ticket
some new version: x.x.22
another change
new version: x.y.2.2

最佳答案

捕获组 1 中的每个记录 ID 和组 2 中的内容

r'(?ms)^(\d{5,6}\r?\n)(.*?)^新版本:'

https://regex101.com/r/A3ejjN/1

关于python - 如何提取数字之间的字符串? (并保留字符串中的第一个数字?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240959/

相关文章:

python - 将字符串添加到 Dataframe Python 中列的所有元素

php - 使用正则表达式替换 smarty 模板系统?

用于提取 FDF 数据的 PHP 正则表达式代码

html - CSS边框问题

excel - 在范围内查找部分文本,返回单元格值

python - 解压大端编码端口号

python - 如何确定 Python 共享库的路径和名称?

PHP 从 url 获取版本

java - 如何将 jTextPane 中的整个(突出显示或不突出显示)文本设置为正常?

python - 为什么 "x = x.append(...)"在 for 循环中不起作用?