Chinaunix

标题: 正则表达式之{}匹配问题 [打印本页]

作者: jkit    时间: 2005-11-26 17:12
标题: 正则表达式之{}匹配问题
现在需要用正则表达式抓取{和与其相匹配的}之间的内容,但是{}里面也可能出现{和}。
例如
{hello} 抓到 hello
{hello {me}} 抓到 hello {me}
如果这样做不到的话,内容里面的{}也可以添加转义符等,比如{hello \{me\}} 抓到 hello {me}或者抓到 hello \{me\} 后再将\去掉都行.
这样的正则表达式该如何写?
作者: limodou    时间: 2005-11-26 21:12
{}在python的正则表达式中是特殊字符,因此需要使用转义符。使用下面的语句:

import re
r = re.compile(r'\{(.*)\}')
b=r.search('asdfd{he{zzz}llo}sfsdf')
b.groups()
作者: jkit    时间: 2005-11-26 23:32
谢谢limodou。
不过'\{(.*)\}'抓到的是第一个{和最后一个}之间的内容,而不是{和与其相匹配的}之间的内容。
比如'{hello {me}} {test}',需要抓到hello {me},用'\{(.*)\}'结果抓到的是'hello {me}} {test'。
作者: limodou    时间: 2005-11-26 23:59
可以改为

r'\{(.*?)\}'

但对于'{hello {me}} {test}'匹配出来的就是:

'hello{me' 了。这种嵌套的不好处理。
作者: 寂寞烈火    时间: 2005-11-27 02:32
原帖由 limodou 于 2005-11-26 21:12 发表
{}在python的正则表达式中是特殊字符,因此需要使用转义符。使用下面的语句:

import re
r = re.compile(r'\{(.*)\}')
b=r.search('asdfd{he{zzz}llo}sfsdf')
b.groups()

python真神奇Y
作者: limodou    时间: 2005-11-27 20:19
神奇吗?就是正则表达式而已呀。
作者: tigerpower    时间: 2005-11-28 18:59
{}前不需要\的
作者: wyting    时间: 2005-12-08 14:09
这个有人搞出来了吗,光用正则表达式能搞出来吗?
作者: wyting    时间: 2005-12-08 15:57

  1. import string

  2. mystring = '{hello{me}}{test}'
  3. mylist = []
  4. mywordlist = []
  5. index = mystring.find('{')
  6. mystart = index
  7. while index<len(mystring):
  8.   if mystring[index] != '}':
  9.     mylist.append(mystring[index])
  10.   else:
  11.     index_j = index
  12.     while index_j >= mystart:
  13.       mychar = mylist.pop()
  14.       index_j = index_j - 1
  15.       if mychar != '{':
  16.         mywordlist.append(mychar)
  17.       else:
  18.         mywordlist.reverse()
  19.         print string.join(mywordlist,'')
  20.         mywordlist.insert(0,'{')
  21.         mywordlist.append('}')
  22.         mywordlist.reverse()
  23.         if len(mylist)==0:
  24.           mywordlist = []
  25.         break
  26.   index = index + 1
复制代码


  1. me
  2. hello{me}
  3. test
复制代码



正则表达式我是搞不出来了,只能用这个方法了。

[ 本帖最后由 wyting 于 2005-12-8 15:58 编辑 ]
作者: jkit    时间: 2005-12-08 18:18
现有的正则表达式估计是不行的了,不知道以后的正则表达式能不能支持计算字符出现的次数。
作者: limodou    时间: 2005-12-08 22:09
可以使用findall之类的求出结果,对其求len()即可。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2