Если все наши желаемые выходные данные похожи на пример, приведенный в вопросе, мы можем, например, определить класс символов ([\w-])
, добавить любые символы, которые нам нравится собирать, затем использовать <\/font>
в качестве левой границы и <br>
как правая граница. Мы также добавили бы группы с необязательными пробелами, и наше выражение выглядело бы так:
<\/font>(\s+)?([\w-]+?)(\s+)?<
или
<\/font>(\s+)?([\w-]+?)(\s+)?<br>
с флагом i
.
Тест
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"<\/font>(\s+)?([\w-]+?)(\s+)?<"
test_str = ("<A Name= 505 ></A> <IMG SRC=\"../../files/heb-anc-sm-pey.jpg\"><IMG SRC=\"../../files/heb-anc-sm-lamed.jpg\"><IMG SRC=\"../../files/heb-anc-sm-aleph.jpg\"> <Font face=\"arial\" size=\"+1\"> אֶלֶף </Font> e-leph <BR> Thousand <BR> Ten times one hundred in amount or number. <BR>Strong's Number: 505 <BR><A HREF=\"audio/ 505 .mp3\"><IMG SRC=\"../../files/icon_audio.gif\" width=\"25\" height=\"25\" border=\"0\"></A><BR> <A HREF=../ahlb/aleph.html#505><Font color=A50000><B>AHLB</B></Font></A> <HR>\n"
" <A Name= 517 ></A> <IMG SRC=\"../../files/heb-anc-sm-mem.jpg\"><IMG SRC=\"../../files/heb-anc-sm-aleph.jpg\"> <Font face=\"arial\" size=\"+1\"> אֵם </Font> eym <BR> Mother <BR> A female parent. Maternal tenderness or affection. One who fulfills the role of a mother. <BR>Strong's Number: 517 <BR><A HREF=\"audio/ 517 .mp3\"><IMG SRC=\"../../files/icon_audio.gif\" width=\"25\" height=\"25\" border=\"0\"></A><BR> <A HREF=../ahlb/aleph.html#517><Font color=A50000><B>AHLB</B></Font></A> <HR>\n")
matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
RegEx
Если это выражение нежелательно или вы хотите изменить его, посетите regex101.com .
RegEx Circuit
jex.im визуализирует регулярные выражения: