I am writing a program using Qt 4.6 and I need to capture all occurences of non-range literals from expressions like
"SUM(A1:A3)+B1-B3+SUM(D1:D3)/COUNT(D1:D3)",
i.e. B1, B3, but not A1, A3, D1, D3. I have tried to use QRegExp, but it doesn’t support negative lookbehind assertions, so I can’t exclude literals like A3, D3.
My regexp (?<!:)([A-Z]{1,4}[1-9]\\d{0,3})(?!:) doesn’t work.
I need your input. Thanks.
I am writing a program using Qt 4.6 and I need to capture all
Share
In your case you could use
(?:^|[^:])\b([A-Z]{1,4}[1-9]\d{0,3})\b(?!:)The first group matches the empty string at the beginning or any character except the colon. I also added word boundaries
\bso that the pattern won’t match things likeA4a.Often times it is simpler to write “positive” patterns. For example, using
with
...denoting your[A-Z]pattern to match cell references, you can match ranges and non-ranges in one pass, then discard all ranges when looping over the results. You can easily detect whether a match is a range by checking if the second capture group is empty.