Search code examples
c++regexqtcaptureqstring

Regex with Qt capture some text


I would like to capture second occurence of the text replaced by a star in the following string:

&nbsp;SW * <br>

ie string starting with &nbsp;SW ending with <br> in a QString, using the RegEx with Qt.

an example here: the string is

&nbsp;&nbsp;SW = min(1, max(0, pow(10, -0.2 - 6.5 ) ** (1.0 / 0.2)))<br>

and expected result is

= min(1, max(0, pow(10, -0.2 - 6.5 ) ** (1.0 / 0.2)))

So far, i have QRegExp rx("^[\&nbsp;SW](.*)[<br>]$"); which is not compiling.

How would you do ?


Solution

  • The compilation issue is probably due to trying to escape the ampersand (\&). But other than that, your regex is mostly right, just overusing character groups ([]), they are not for grouping. This expression works in my tests: &nbsp;SW(.*)<br>, so in your case you'd do something like

    QRegExp rx("&nbsp;SW(.*)<br>")