I am trying to match google urls from some text that is stored in a variable, using the pattern below.

The urls use double quotes

QRegExp regExp;
regExp.setPattern("http://www.google.com/(.*)");

I manage to match the url but it unwontedly matches all of the text that is contained after it. I have tried using similar variants like the ones below, but they don't seem to work.

regExp.setPattern("http://www.google.com/(.*)\"is"); 
regExp.setPattern("http://www.google.com/^(.*)$\"");

Any help to get a regular expression that matches just the url alone.

Thanks in advance

有帮助吗?

解决方案

Even though it is impossible for us to know what is around the urls in your text (quotes ? parenthesis ? white spaces ?), we can create a better regular expression by trying to do a negative match of characters that cannot be part of the url:

QRegExp regExp;
regExp.setPattern("http://www.google.com/([^()\"' ]*)");

Then you just need to add more possible characters to this negative character class.

其他提示

Is there a reason you need/want to use a QRegExp?

You could use a QUrl most likely.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top