문제

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