2

テキスト内の順序 (a)、(b)、(c) をキャプチャする正規表現を作成し、その前に次のように改行を追加する必要があります。

ISSSS の統合プログラム フレームワークは、次の目的で確立されました。(b) 武装集団の動員解除と再統合を支援する。(c) 法の支配と公の秩序を維持するための国家公務員 (警察、刑務所、司法、行政) の訓練と配備を通じて、以前は武装集団によって支配されていた地域に国家機能を再確立する。 (d) 道路への開放を確保する。およびインフラストラクチャ。(e) 国内避難民および難民の安全で威厳のある帰還を促進する。(f) 優先的な社会的ニーズと紛争の主な原因に対処し、経済回復を開始する。

それを数字 (1),(2),(3)... とローマ数字 (I),(II),(III).../ (i),(ii), (ⅲ)

さらなる課題として、A- B- C- または ABC または A) B) C) などの任意のスタイルに一致させることはできますか?

何か案は?

4

2 に答える 2

1
print re.sub("\((I+|i+|[a-z0-9])\)","\n\g<0>",buff)

発生します

The Integrated Programme Framework of the ISSSS has been established to: 
(a) create a protective environment for civilians by strengthening the security forces,     and improving discipline and control; 
(b) support the demobilization and reintegration of armed groups; 
(c) re-establish state functions in areas formerly controlled by armed groups, through     the training and deployment of state officials (police, penitentiary, judicial     and administration) to uphold the rule of law and public order, 
(d) ensure open road access and infrastructure; 
(e) promote a safe and dignified return of internally displaced persons and refugees; and 
(f) address priority social needs and key sources of conflict and initiate economic recovery.

「再確立」などのハイフンでつながれた単語の問題に遭遇したという理由だけで、完全に「普遍的な」ものを作成することに私は懐疑的です。上記の正規表現は大部分のケースをカバーするはずであり、「|」内のものを追加または削除することで簡単に調整できるはずです オペレーター。そうは言っても、もう少し遊んでテスト文を追加したところ、十分に機能しました。「)」、「.」、または「-」が使用されているなどの潜在的な問題を回避するために、スペースから始めたと仮定する必要がありました。

>>> buff += "This is a) test of i) one ii) two iii) three a. four and b- five"
>>> print re.sub(" \({0,1}(I+|i+|[a-zA-Z0-9])(\)|\.|-)","\n\g<0>",buff)

The Integrated Programme Framework of the ISSSS has been established to:
 (a) create a protective environment for civilians by strengthening the security forces, and improving discipline and control;
 (b) support the demobilization and reintegration of armed groups;
 (c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order,
 (d) ensure open road access and infrastructure;
 (e) promote a safe and dignified return of internally displaced persons and refugees; and
 (f) address priority social needs and key sources of conflict and initiate economic recovery.This is
 a) test of
 i) one
 ii) two
 iii) three
 a. four and
 b- five
于 2013-04-10T18:07:37.370 に答える
1

コールバックで sub を使用できます。

import re

subject = 'The Integrated Programme Framework of the ISSSS has been established to: (a) create a protective environment for civilians by strengthening the security forces, and improving discipline and control; (b) support the demobilization and reintegration of armed groups; (c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order, (d) ensure open road access and infrastructure; (e) promote a safe and dignified return of internally displaced persons and refugees; and (f) address priority social needs and key sources of conflict and initiate economic recovery.'

def callback_f(e):
    # Check your input
    if e.group()[1] == 'a':
        print(e.group()[1])
        return '(1)'
    else:
        print(e.group()[1])
        return '(' + e.group()[1] + ')';

result = re.sub(r'\((\w)\)', callback_f, subject)

print(result)

私はこれに大々的には入りません。a/b/c/d/eただし、辞書を使用して、値をマッピングで一致するものに自動的に置き換えることができます。

このデモはターミナルに出力されます。

192:Desktop allendar$ python test.py 
a
b
c
d
e
f
The Integrated Programme Framework of the ISSSS has been established to: A create a protective environment for civilians by strengthening the security forces, and improving discipline and control; (b) support the demobilization and reintegration of armed groups; (c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order, (d) ensure open road access and infrastructure; (e) promote a safe and dignified return of internally displaced persons and refugees; and (f) address priority social needs and key sources of conflict and initiate economic recovery.
于 2013-04-10T18:05:39.933 に答える