正则表达式问题,如果单行 [英] Regex problem if single line

查看:96
本文介绍了正则表达式问题,如果单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从大文本中获取视频的类型和ID,为此,我将Regex与以下表达式配合使用

I need to get the type and id of videos from a big text, so to do that I used Regex with following expression

video\(''(?<type>.*),(?<id>.*)''\);




现在,如果文字是类似的




now if the text is something like

video(''abc,efg'');
some text
video(''abc,efg'');
some text
video(''abc,efg'');
some text
video(''abc,efg'');



它有效

但是如果文本只有一行,例如



it works

but if text has single line, like

video(''abc,efg'');some text video(''abc,efg'');some text video''abc,efg'');some text 
video(''abc,efg'');



它不起作用,返回错误值...



it doesn''t work, returns wrong values...

推荐答案

.*是贪婪",但是.*?是懒惰".贪婪的版本将找到最长的匹配项.惰性版本将找到最短的匹配项.您想要的是惰性版本.

实际上,您的问号应该没问题.我忘记了该构造的作用,但是我之前已经看过它.




由Asker
工作中的人
.* is "greedy", but .*? is "lazy". The greedy version will find the longest match. The lazy version will find the shortest match. What you want here is the lazy version.

Actually, your question marks should be fine. I forget what that construct does, but I''ve seen it before.




Edit by Asker :
the working one
video\(''(?<type>.*?),(?<id>.*?)''\);



谢谢



thanks


用始终拒绝''的语句替换.*.您的正则表达式将继续运行,直到找到最后一个报价,而不是下一个报价.我忘记了那是什么,但是您可以使用易于使用Google的expresso来帮助构建和测试正则表达式.
Replace the .* with a statement that always rejects a ''. Your regex will keep going on the line until it finds the last quote, not the next one. I forget what that is, but you can use expresso, which is easy to google, to help build and test regex.


这篇关于正则表达式问题,如果单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆