Chinaunix

标题: 正则表达式怎么用。。 [打印本页]

作者: pandaiam    时间: 2018-08-03 12:43
标题: 正则表达式怎么用。。
现在有这个字符串
jakee and zz done.<p>zzz</p> <p style=\"text-align: center;\" align=\"justify\">==TUIGUANG===</p>\n<p align=\"justify\"><img style=\"width: auto;\" src=\"https://imag.xxxx.com/b931035c-5bf2-4553-9593-07480f74a5c6.png\" /></p> this should not dispeared <p>ALL MY ...</p> <p > See you</p> behan


现在用
  1. regexp.MustCompile(`<p.*?TUIGUANG.*?</p>`)
复制代码

匹配,愿意是想匹配到
<p style=\"text-align: center;\" align=\"justify\">==TUIGUANG===</p>
结果是直接匹配到了
<p>zzz</p> <p style=\"text-align: center;\" align=\"justify\">==TUIGUANG===</p>
有什么写法可以只匹配到最短的那段吗。。

我不知道别的正则是不是支持这种匹配,引用程序是用go写的,就先关注go的regexp了。

谢谢。



作者: lxyscls    时间: 2018-08-08 10:51
回复 1# pandaiam
...
作者: lxyscls    时间: 2018-08-08 10:51
本帖最后由 lxyscls 于 2018-08-08 10:53 编辑

回复 1# pandaiam
In [11]: r = re.search('<p\ .*?TUIGUANG.*?</p>', 'jakee and zz done.<p>zzz</p> <p style=\"text-align: center;\" align=\"justify\">==TUIGUANG===</p>\n<p align=\"justify\"><img style=\"width: auto;\" src=\"https://imag.xxxx.com/b931035c-5bf2-4553-9593-07480f74a5c6.png\" /></p> this should not dispeared <p>ALL MY ...</p> <p > See you</p> behan')

In [12]: r.group()
Out[12]: '<p style="text-align: center;" align="justify">==TUIGUANG===</p>'

python







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