私はいくつかの顧客のコメントに文法規則に従わなかったものがあります。例えば、前の文についてのより多くの説明を提供する以下の文中の(Such as s and b.)
は、2つの点で囲まれている。カッコの前にドットをスペースで置き換える正規表現
text = "I was initially scared of ANY drug after my experience. But about a year later I tried. (Such as s and b.). I had a very bad reaction to this."
まず、私は. (Such as s and b.).
を検索し、空間に(Such as s and b.)
前にドットを交換したいです。これは私のコードですが、動作しません。
text = re.sub (r'(\.)(\s+?\(.+\)\s*\.)', r' \2 ', text)
出力は次のようになります。
"I was initially scared of ANY drug after my experience. But about a year later I tried (Such as s and b.). I had a very bad reaction to this."
私のpythonを使用しています。
上で実行を参照してください、あまりにも、常にそこに '' \ 'です?私はパターンにそれらを含める必要があると思います。 –
@WiktorStribiżew、いいえ、私はテキストを編集しました。 – Mary
あなたのパターンは 'r '(\。)(\ s + \(。+?\)\ s * \)' 'でなければならず、置換は' r' \ 2''である必要があります。または、これを試してみてください。['re '(r' \。(\ s + \([^)] * \)\ s * \。) '、r' \ 1 '、text)'](http:// ideon.com/rFGpqj) –