我试图用忽略案例捕获多个组,并且暂时没有任何进展。我正在检查的字符串可以有多种形式,例如一些
<A title="Test title Ch.42" href="http://www.google.com">Test title Ch.42 </a>
<A title="Test title Vol2. Ch.42" href="http://www.google.com">Test title Vol2. Ch.42 </a>
<A title="Test title Vol2.Ch.42" href="http://www.google.com">Test title Vol2.Ch.42 </a>
<A title="Test title \"with multiple quotes\" Ch.42" href="http://www.google.com">Test title "with multiple quotes" Ch.42 </a>
<A title="Test title w1th numb3rs Ch.42" href="http://www.google.com">Test title w1th numb3rs Ch.42 </a>
<A title="Test title no 42" href="http://www.google.com">Test title no 42 </a>
所以一般来说规则是这样的:
标题标签中的主标题可以包含每个字符,包括数字和特殊字符
该 url 是标准 url,但可以使用 (.*) 表达式捕获而不会出现问题
Ch。一般是可选的
如果字符串包含 Vol.,则 Ch. 强制执行
我当前的正则表达式如下所示:
pattern = re.compile('<A title="((.*)(?:Vol.[\d]+){0,1}(?: Ch.){0,1}([\d]+))" href="(.*)">')
我想尝试捕捉:
带有 Vol 和 Ch 的标题标签,包括它们后面的数字
没有 Vol 和 Ch 的标题(并且没有 Vol 和 Ch 后面的数字)
Ch后面的数字。
拆分正则表达式会更好,什么对性能更好(它运行了几千个字符串,所以我想保持它的性能)?
亲切的问候 Baumchen