in_array()
- это то, что я использую
if (in_array($variable, array('one','two','three'))) {
Если вы хотите захватить каждый компонент, вы можете сгруппировать их для каждого. Проверьте здесь .
import re
mylist = ['2.1 [ii] Agreement and Plan of Reorganization, by and among the Company, Force Acq. Corp. and Force Computers, Inc. as amended.',
'3.1 [viii] Articles of Incorporation of Company, as amended.',
'3.2 [viii] Bylaws of Company.',
'10.1 [I] Preferred Stock Purchase Agreement dated September 29, 1983, together with amendments thereto dated February 28, 1984 and',
'10.2 [I] Form of Indemnification Agreement between Company and its officers, directors and certain other key employees.'
]
for item in mylist:
regex = re.search('(?P<chapter>\d+[.]\d+)\s+(?P<subchapter>\[.*\])\s+(?P<title>.*)', item)
regex = regex.groups()
print (regex[0], regex[1], regex[2])
Выход:
2.1 [ii] Agreement and Plan of Reorganization, by and among the Company, Force Acq. Corp. and Force Computers, Inc. as amended.
3.1 [viii] Articles of Incorporation of Company, as amended.
3.2 [viii] Bylaws of Company.
10.1 [I] Preferred Stock Purchase Agreement dated September 29, 1983, together with amendments thereto dated February 28, 1984 and
10.2 [I] Form of Indemnification Agreement between Company and its officers, directors and certain other key employees.
Если вы просто хотите, чтобы каждый абзац был элементом, я предлагаю следующее:
import re
text = """ 2.1 [ii] Agreement and Plan of Reorganization, by and among the Company,
Force Acq. Corp. and Force Computers, Inc. as amended.
3.1 [viii] Articles of Incorporation of Company, as amended.
3.2 [viii] Bylaws of Company.
10.1 [I] Preferred Stock Purchase Agreement dated September 29, 1983,
together with amendments thereto dated February 28, 1984 and
10.2 [I] Form of Indemnification Agreement between Company and its
officers, directors and certain other key employees.
10.3 [I] Amendment to form of Indemnification Agreement.
10.4 [iv] 1983 Incentive Stock Option Plan, as amended August 13, 1991.
10.5 [vi] 1988 Employee Stock Purchase Plan, as amended October 1992.
10.6 [v] Amended and Restated 1992 Stock Option Plan."""
text = re.findall(r"\d{1,2}\.\d+.*?(?=\d{1,2}\.\d+|$)", text, re.S)
for paragraph in text:
print(paragraph)
Это приводит к:
2.1 [ii] Agreement and Plan of Reorganization, by and among the Company,
Force Acq. Corp. and Force Computers, Inc. as amended.
3.1 [viii] Articles of Incorporation of Company, as amended.
3.2 [viii] Bylaws of Company.
10.1 [I] Preferred Stock Purchase Agreement dated September 29, 1983,
together with amendments thereto dated February 28, 1984 and
10.2 [I] Form of Indemnification Agreement between Company and its
officers, directors and certain other key employees.
10.3 [I] Amendment to form of Indemnification Agreement.
10.4 [iv] 1983 Incentive Stock Option Plan, as amended August 13, 1991.
10.5 [vi] 1988 Employee Stock Purchase Plan, as amended October 1992.
10.6 [v] Amended and Restated 1992 Stock Option Plan.
Ключ - ? [ 116] позади . * , так что оценка ленива. Это означает, что регулярное выражение соответствует всему, что должно, но не больше. Если вы оставите ? , он будет соответствовать остальной части строки.
(? = ...) позволяет вам опустить регулярное выражение в своем результате, так что вы просто сопоставляете все до следующего абзаца. Надеюсь, это поможет.