yakczh_cu 发表于 2015-04-08 18:06

re怎么同时匹配多行注释和单行注释

/* 44:   */   catch (Exception e)
/* 45:   */   {
/* 46:68 */       mResult = new MobileResult();
/* 47:69 */       mResult.setResult(-1);
/* 48:70 */       mResult.setMsg("服务器异常!");
/* 49:71 */       this.log.error("获取配置信息出现异常", e);
/* 50:   */   }
/* 51:73 */   return this.gson.toJson(mResult);
/* 52:   */   }
/* 53:   */ }


/* Location:            webapps\gams\WEB-INF\classes\
* Qualified Name:   com.xx.gams.ci.ciconfigInfo.ConfigInfoController
* JD-Core Version:    0.7.0.1
*/比如这样的代码

haooooaaa 发表于 2015-04-09 10:12

这个似乎有点麻烦,

你看看这个

abian.blog.51cto.com/751059/1619707

lin_bird 发表于 2015-04-09 21:52

>>> string = '''/* 44:   */   catch (Exception e)
/* 45:   */   {
/* 46:68 */       mResult = new MobileResult();
/* 47:69 */       mResult.setResult(-1);
/* 48:70 */       mResult.setMsg("服务器异常!");
/* 49:71 */       this.log.error("获取配置信息出现异常", e);
/* 50:   */   }
/* 51:73 */   return this.gson.toJson(mResult);
/* 52:   */   }
/* 53:   */ }


/* Location:            webapps\gams\WEB-INF\classes\
* Qualified Name:   com.xx.gams.ci.ciconfigInfo.ConfigInfoController
* JD-Core Version:    0.7.0.1
*/'''
>>> print string
/* 44:   */   catch (Exception e)
/* 45:   */   {
/* 46:68 */       mResult = new MobileResult();
/* 47:69 */       mResult.setResult(-1);
/* 48:70 */       mResult.setMsg("服务器异常!");
/* 49:71 */       this.log.error("获取配置信息出现异常", e);
/* 50:   */   }
/* 51:73 */   return this.gson.toJson(mResult);
/* 52:   */   }
/* 53:   */ }
>>> import re
>>> m = re.findall(r'/\*.*?\*/',string,re.DOTALL)
>>> for item in m:
        print item

/* 44:   */
/* 45:   */
/* 46:68 */
/* 47:69 */
/* 48:70 */
/* 49:71 */
/* 50:   */
/* 51:73 */
/* 52:   */
/* 53:   */
/* Location:            webapps\gams\WEB-INF\classes * Qualified Name:   com.xx.gams.ci.ciconfigInfo.ConfigInfoController
* JD-Core Version:    0.7.0.1
*/

CUTianrui007 发表于 2015-04-28 21:37

这个好像在《正则指引》中有提到过,用到了忽略优先量词。
//注释://.*
/**/注释:/\*[\s\S]*?;\*/
匹配时使用单行模式,使用*?忽略优先量词会一直匹配到最后的*/,这样就可以找到所有的位于两个*号间的注释。

icymirror 发表于 2015-04-29 08:18

回复 1# yakczh_cu
如果是只匹配/* */这种格式的注释,可以用下面这个正则表达式:

r"/\*[\w\W]*?\*/"
说明:
/\*: 注释开始时的 /*
[\w\W]*?: 用非贪婪模式匹配任意长度任意字符。(非贪婪模式指:尽可能少的去匹配,这时,后面的匹配结束部分的标识会尽可能早的起作用。)
\*/:匹配注释结束的 */
页: [1]
查看完整版本: re怎么同时匹配多行注释和单行注释