[问题] regex 的 \1 抓不到字串

楼主: Neisseria (Neisseria)   2014-06-16 10:38:23
小弟我重写书上一个将字串相接的小程式
部分程式码如下 http://pastebin.com/c4GUJ4UW
但目前错误未修正,仅供参考
原来的想法是将两段字串相接起来,像是
字串1: merely players;\nThey have
字串2: They have their exits and their entrances,\n
相接的方式为 "".join([字串1, 'XXX', 字串2]) 得到字串3 如下
merely players;\nThey haveXXXThey have their exits and their entrances,\n
然后用 regex 得到重叠的部分
match = re.match(r'([\S ]{2,})XXX\1', 字串3)
if match:
print match.group(1)
理论上应该输出 "They have" 才是,却一直 match 不到
我还刻意用 Perl 以同样的 regex pattern 跑,可以抓得到 $1
(假设将字串3 用 print 输出到 stdout)
$ python file.py | perl -n -e 'print "$_$1\n" if /([\S ]{2,})XXX\1/;'
看来似乎也不是 pattern 的问题?
希望各位大大可以解惑,感恩
作者: uranusjr (←這人是超級笨蛋)   2014-06-16 12:55:00
不是用 match; read the doc http://d.pr/eqj6
作者: timTan (用口头禅区分年记)   2014-06-16 17:49:00
match 很严格的,换一个~
楼主: Neisseria (Neisseria)   2014-06-16 21:38:00
看来我对于 Python 的 Regex 还得多学学~

Links booklink

Contact Us: admin [ a t ] ucptt.com