0

正規表現をセパレータとして使用して文字列を分割しようとしましたが、の出力にはstring.split冗長な結果が含まれているようです。

import re;
replaceArray = '((Replace the string)|((in|inside|within) the string)|(with the string))'
stringToSplit = '(Replace the string arr1 in the array arr2 with the array arr3)'
print(re.split(replaceArray, stringToSplit))

結果が重複することなく、分割文字列が次のようになると予想しました。

['Replace the string', ' arr1 ', 'in the string', ' arr2 ', 'with the string', ' arr3']

しかし代わりに、分割された文字列の配列には冗長な結果が含まれており、一致した他の文字列と重複しているように見えます。

['', 'Replace the string', 'Replace the string', None, None, None, ' arr1 ', 'in the string', None, 'in the string', 'in', None, ' arr2 ', 'with the string', None, None, None, 'with the string', ' arr3']

これらの冗長で重複する結果が の出力に含まれないようにする方法はありますstring.splitか?

4

3 に答える 3

2

正規表現にキャプチャ グループがある場合、 の結果にre.split()はそれらのキャプチャ グループが含まれます。すべてのグループの先頭に追加?:して、それらを非キャプチャにします。これらのグループのいくつかは実際には必要ありません。次のことを試してください。

replaceArray = 'Replace the string|(?:in|inside|within) the string|with the string'
于 2013-09-19T20:11:00.490 に答える
1

ドキュメントからre.split:_

pattern でキャプチャ用括弧が使用されている場合、パターン内のすべてのグループのテキストも結果のリストの一部として返されます。

正規表現で非キャプチャ グループを使用したいと思います。つまり、 を(...)使用する代わりに、(?:...)

于 2013-09-19T20:11:36.130 に答える
1

が先頭に付いている一致するグループ?:は非キャプチャ グループであり、出力には表示されません。re.splitさらに、おそらくここでは使用したくないでしょうが、re.match代わりに、文字列を分割することにあまり関心がなく、その代わりにそれらのグループを抽出したいと考えています。

>>> expr = '\((Replace the array (.*?)) ((?:in|inside|within) the array (.*?)) (with the array (.*?))\)'
>>> re.match(expr, stringToSplit).groups()
('Replace the array arr1', 'arr1', 'in the array arr2', 'arr2', 'with the array arr3', 'arr3')

または

>>> expr = '\((Replace the array) (.*?) ((?:in|inside|within) the array) (.*?) (with the array) (.*?)\)'
>>> re.match(expr, stringToSplit).groups()
('Replace the array', 'arr1', 'in the array', 'arr2', 'with the array', 'arr3')
于 2013-09-19T20:14:10.900 に答える