正则表达式,用于匹配各种类型的编号列表

编程入门 行业动态 更新时间:2024-10-28 20:31:34
本文介绍了正则表达式,用于匹配各种类型的编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个(PCRE)正则表达式以匹配所有常用的编号列表,并且我想分享自己的想法并收集输入信息的方式.

I'd like to create a (PCRE) regular expression to match all commonly used numbered lists, and I'd like to share my thoughts and gather input on way to do this.

我已将列表"定义为英国盎格鲁-撒克逊规范化规范集,即

I've defined 'lists' as the set of canonical Anglo-Saxon conventions, i.e.

1 2 3 1. 2. 3. 1) 2) 3) (1) (2) (3) 1.1 1.2 1.2.1 1.1. 1.2. 1.3. 1.1) 1.2) 1.3) (1.1) (1.2) (1.3)

字母

a b c a. b. c. a) b) c) (a) (b) (c) A B C A. B. C. A) B) C) (A) (B) (C)

罗马数字

i ii iii i. ii. iii. i) ii) iii) (i) (ii) (iii) I II III i. ii. iii. i) ii) iii) (i) (ii) (iii)

我想知道列表的强度,是否应该有其他编号约定,以及是否应删除其中的任何编号.

I'd like to know how strong a set of list this is, and if there are other numbering conventions that should be in there, and if any of these ought to be removed.

这是我为解决此问题而创建的正则表达式(在 Python 中):

Here's a regular expression I've created to solve this problem (in Python):

numex = r'(?:\d{1,3}'\ # 1, 2, 3 '(?:\.\d{1,3}){0,4}'\ # 1.1, 1.1.1.1 '|[A-Z]{1,2}'\ # A. B. C. '|[ivxcl]{1,6}' # i, iii, ... rex = repile(r'(\(?%s\)|%s\.?)' % numex, re.I) # re.U? rex.match("123. Some paragraph")

我想知道此正则表达式是否足以解决此问题,以及是否还有其他替代方法(正则表达式或其他方法).

I'd like to know how adequate this regex is for this problem, and if there are other alternative (regex or otherwise) solutions.

顺便说一句,对于我的特定用例,我预计列表编号不会超过25-50.

Incidentally, for my particular use-case, I wouldn't expect list numbers of more than 25-50.

感谢您阅读.

布莱恩

推荐答案

以下是Wikified解决方案:

numex = r"""^(?: \d{1,3} # 1, 2, 3 (?:\.\d{1,3}){0,4} # 1.1, 1.1.1.1 | [B-H] | [J-Z] # A, B - Z caps at 26. | [AI](?!\s) # Note: "A" and "I" can properly start non-lists | [a-z] # a - z | [ivxcl]{1,6} # Roman ii, etc | [IVXCL]{1,6} # Roman IV, etc. ) """ rex = repile(r'^\s*(\(?%s\)|%s\.?)\s+(.*)' % (numex, numex), re.X)

最欢迎添加,更改和建议.

Additions, changes and suggestions most welcome.

更多推荐

正则表达式,用于匹配各种类型的编号列表

本文发布于:2023-11-03 19:47:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编号   类型   列表   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!