Python正则表达式-在文件中查找一个字符串,该字符串出现在另一个字符串之前?

Python正则表达式-在文件中查找一个字符串,该字符串出现在另一个字符串之前?,python,regex,Python,Regex,我的编程知识是非常有限的,我将非常感谢任何帮助这个可能明显的问题 假设我有一个文本文件,其中某处包含文本:“我拥有两辆自行车(中间的一些文本…) 例如,我如何将二改为三?这意味着我需要一个函数来找到字符串“bicycles”,然后向左看,直到它在某个地方找到字符串“two”并改变它。您可以使用正则表达式来实现这一点: >>> import re >>> s = 'I own two (Some text in between...) bicycles and

我的编程知识是非常有限的,我将非常感谢任何帮助这个可能明显的问题

假设我有一个文本文件,其中某处包含文本:“我拥有两辆自行车(中间的一些文本…


例如,我如何将二改为三?这意味着我需要一个函数来找到字符串“bicycles”,然后向左看,直到它在某个地方找到字符串“two”并改变它。

您可以使用正则表达式来实现这一点:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
或常规字符串函数:

>>> try:
...   p = s.rindex('two', 0, s.index('bicycles'))
...   s[:p] + 'three' + s[p+len('two'):]
... except ValueError:
...   pass # No bicycles or no two
...
'I own three (Some text in between...) bicycles and two dogs.'

可以使用正则表达式执行此操作:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
或常规字符串函数:

>>> try:
...   p = s.rindex('two', 0, s.index('bicycles'))
...   s[:p] + 'three' + s[p+len('two'):]
... except ValueError:
...   pass # No bicycles or no two
...
'I own three (Some text in between...) bicycles and two dogs.'

对于正则表达式:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
结果

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs 


-------------------------------------------------------------
1) Replacing the more at right before the first "BICYCLES":

I gave two similar things to my two twin sons: two spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
2) Replacing the more at right before the last "BICYCLES":

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
3) Replacing all before the first "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
4) Replacing all before the last "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for @@@@ horridly nice BICYCLES, @@@@ times 300 dollars for @@@@ sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES because his two sons wanted only two BICYCLES 


Replacing all strings "two" before the first "BICYCLES":

Mr Dotwo bought @@@@ gifts for his @@@@ sons, @@@@ hours ago: @@@@ BICYCLES because his two sons wanted only two BICYCLES

也可以不使用正则表达式:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
结果是一样的

但是使用正则表达式可以提供更多的可能性:

import re

ss = ('Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES '
      'because his two sons wanted only two BICYCLES')
print ss,'\n\n'


print 'Replacing all "two" before the first "BICYCLES":\n'
reg = re.compile('(\\btwo\\b)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
结果

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs 


-------------------------------------------------------------
1) Replacing the more at right before the first "BICYCLES":

I gave two similar things to my two twin sons: two spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
2) Replacing the more at right before the last "BICYCLES":

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
3) Replacing all before the first "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
4) Replacing all before the last "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for @@@@ horridly nice BICYCLES, @@@@ times 300 dollars for @@@@ sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES because his two sons wanted only two BICYCLES 


Replacing all strings "two" before the first "BICYCLES":

Mr Dotwo bought @@@@ gifts for his @@@@ sons, @@@@ hours ago: @@@@ BICYCLES because his two sons wanted only two BICYCLES

对于正则表达式:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
结果

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs 


-------------------------------------------------------------
1) Replacing the more at right before the first "BICYCLES":

I gave two similar things to my two twin sons: two spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
2) Replacing the more at right before the last "BICYCLES":

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
3) Replacing all before the first "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
4) Replacing all before the last "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for @@@@ horridly nice BICYCLES, @@@@ times 300 dollars for @@@@ sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES because his two sons wanted only two BICYCLES 


Replacing all strings "two" before the first "BICYCLES":

Mr Dotwo bought @@@@ gifts for his @@@@ sons, @@@@ hours ago: @@@@ BICYCLES because his two sons wanted only two BICYCLES

也可以不使用正则表达式:

>>> import re
>>> s = 'I own two (Some text in between...) bicycles and two dogs.'
>>> re.sub('two(.*bicycles)', 'three\\1', s)
'I own three (Some text in between...) bicycles and two dogs.'
import re

line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES)(.+)')
print reg.sub('@@@@\\1',ss)


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
reg = re.compile('two(?=(?:.(?!two))*?BICYCLES(?!.*?BICYCLES))')
print reg.sub('@@@@',ss)


print line + '3) Replacing all before the first "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)


print line + '4) Replacing all before the last "BICYCLES":\n'
reg = re.compile('(two)|BICYCLES(?!.*?BICYCLES).+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
line = '-------------------------------------------------------------\n'

ss = ('I gave two similar things to my two twin sons: '
      'two spankings, two nice BICYCLES of 300 dollars each, '
      'yes 600 dollars for two horridly nice BICYCLES, '
      'two times 300 dollars for two sons receiving two BICYCLES !, '
      'two dollars too, but never two dogs')
print ss,'\n\n'


print line + '1) Replacing the more at right before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '2) Replacing the more at right before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print '@@@@'.join(ss[0:fb].rsplit('two',1)) + ss[fb:] if fb+1 else ss


print line + '3) Replacing all before the first "BICYCLES":\n'
fb = ss.find('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss


print line + '4) Replacing all before the last "BICYCLES":\n'
fb = ss.rfind('BICYCLES')
print ss[0:fb].replace('two','@@@@') + ss[fb:] if fb+1 else ss
结果是一样的

但是使用正则表达式可以提供更多的可能性:

import re

ss = ('Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES '
      'because his two sons wanted only two BICYCLES')
print ss,'\n\n'


print 'Replacing all "two" before the first "BICYCLES":\n'
reg = re.compile('(\\btwo\\b)|BICYCLES.+')
print reg.sub(lambda mat: '@@@@' if mat.group(1) else mat.group(),ss)
结果

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs 


-------------------------------------------------------------
1) Replacing the more at right before the first "BICYCLES":

I gave two similar things to my two twin sons: two spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
2) Replacing the more at right before the last "BICYCLES":

I gave two similar things to my two twin sons: two spankings, two nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
3) Replacing all before the first "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for two horridly nice BICYCLES, two times 300 dollars for two sons receiving two BICYCLES !, two dollars too, but never two dogs
-------------------------------------------------------------
4) Replacing all before the last "BICYCLES":

I gave @@@@ similar things to my @@@@ twin sons: @@@@ spankings, @@@@ nice BICYCLES of 300 dollars each, yes 600 dollars for @@@@ horridly nice BICYCLES, @@@@ times 300 dollars for @@@@ sons receiving @@@@ BICYCLES !, two dollars too, but never two dogs
Mr Dotwo bought two gifts for his two sons, two hours ago: two BICYCLES because his two sons wanted only two BICYCLES 


Replacing all strings "two" before the first "BICYCLES":

Mr Dotwo bought @@@@ gifts for his @@@@ sons, @@@@ hours ago: @@@@ BICYCLES because his two sons wanted only two BICYCLES

不能用简单的字符串函数吗?不能用简单的字符串函数吗?
“我有两辆(文本…)自行车和两条狗。”
@Nitish它代表括号中的所有内容,即
*自行车的匹配结果
@Dor Good point。添加了一个使用普通字符串函数的替代方法,该方法没有此问题。我担心即使使用lookarounds,正则表达式也会非常复杂。
“我有两辆(文本…)自行车和两条狗。”
@Nitish它代表括号中的所有内容,即
*bicycles
@Dor Good point的匹配结果。添加了一个使用普通字符串函数的替代方法,该方法没有此问题。恐怕即使使用lookarounds,正则表达式也会非常复杂。