Вопрос

Given a QString that can have an unknown number of URL's in it...

How can I use QRegExp to wrap HTML anchor tags around only the URL portions (with the URL itself as clickable label).

e.g.

input: "this is www.cnn.com, that is https://www.mybank.com"

output: "this is <a href="www.cnn.com">www.cnn.com</a>, that is <a href="https://www.mybank.com">https://www.mybank.com</a>
Это было полезно?

Решение

ok. got it.

QRegExp regExp("((([A-Za-z]{3,9}:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%\\/\\.\\w\\-]*)?\\??(?:[\\-\\+=&;%@\\.\\w]*)#?(?:[\\.\\!\\/\\\\\\w]*))?)");

QString result = myOriginalString.replace(regExp, "<a href='\\1'>\\1</a>" );

I'll leave the question/answer here for the sake of who may be interested in this.

Другие советы

I'm not going to write your regexp for you. However, Qt 4 comes packaged with a tool that helps a LOT with writing one, which you can also compile yourself.

Alternatively, there should be a set of examples and demos included with your Qt installation (On Windows, go to Start -> Qt Examples and Demos). Fire up the examples application and navigate to Tools (second page) -> Regular Expressions. Click Launch and follow the directions.

Use that demo to help you write one out, following guidelines in the documentation. In particular, look into the capturing text, wildcard matching, and character sets sections.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top