I have a QString of rich text more or less in this format:
<span background-color="red"><a name='item1'></a> property1 </span> + <span background-color="blue"><a name='item2'></a> property2 </span>
It can have more tags, but all will have the same structure. Also, between each tag, operators will show up - this is a string that is supposed to represent a calculation.
I need a regex to traverse the string and extract both the item1
, item2
, ...; but also the property1
, property2
,... substrings so I can then retrieve a value which I have stored somewhere else.
Then, after retrieving these values
, and if, for example, property1=value1 and property2=value2 , I need to create another string like:
value1
+ value2
This string will be evaluated to compute the calculation.
What would be the regex to read the string?
What would be the regex to replace in a copied string?
NOTE I do not intend to parse HTML with these regexps. The string of rich-text I need to filter has at most the tags and structure represented above. It will not have other types of tags, nor will it have other attributes besides the ones in the example string above. It can only have more examples of that same tag structure: a span, containing an anchor tag with a name attribute and some text to display.
NOTE2 @Passerby posted in the comments of this question a link to a very aproximate solution. I forgot one (hopefully small) detail about my objective: I also need to catch whatever is between the span
tags as a string as well, instead of simply checking for a char
like @Passerby (very well) suggested. Any ideas?
NOTE3 I actually still argue that this is not the same question as the duplicate marked one. While the strings I am filtering look like HTML, they are actually rich-text. They will always have this rigid structure/format, so RegEx is perfectly viable for what I need to do. After some great comments I got from a few users, namely @Passerby, I decided to go for it and this works perfectly for what I need:
Sample string:
<span background-color="red"><a name='item1'></a> property1 </span> + 300 * <span background-color="blue"><a name='item2'></a> property2 </span> + Math.sqrt(<span background-color="green"><a name='item3'></a> property3 </span>)
Regex:
/ <span.*?><a name='(.*?)'><\/a>\s*(.*?)\s*<\/span>(((.*?)?)(?=<)|) / g
Outputs:
MATCH 1
1. [38-43] `item1`
2. [50-59] `property1`
3. [67-76] ` + 300 * `
4. [67-76] ` + 300 * `
5. [67-76] ` + 300 * `
MATCH 2
1. [115-120] `item2`
2. [127-136] `property2`
3. [144-157] ` + Math.sqrt(`
4. [144-157] ` + Math.sqrt(`
5. [144-157] ` + Math.sqrt(`
MATCH 3
1. [197-202] `item3`
2. [209-218] `property3`
3. [226-226] (null, matches any position)