Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, March 2007
QRegExp minimal match


Message 1 in thread

Hi,

with setMinimal(true) and the string "abc" I expected the QRegExp 
"^(.*)(.)?(.)?$" to set the submatches $1=a, $2=b and $3=c.

But it results in $1=abc. Why ?

Using Qt 4.2.2.

cu, Bernd
-- 
 [ signature omitted ] 

Message 2 in thread

Hi Bernd,

> with setMinimal(true) and the string "abc" I expected the QRegExp 
> "^(.*)(.)?(.)?$" to set the submatches $1=a, $2=b and $3=c.
> 
> But it results in $1=abc. Why ?

The minimal matching is "global", i.e. it tries to find the shortest 
match possible for the entire string. The individual matches within the 
string are still performed as usual.

I guess this begs the question: Why does't QRegExp support minimal 
quantifiers, e.g., *? and +?. The short answer is that is very difficult 
to accomplish with the current regexp engine. In the long term, I'd like 
us to rewrite the engine (or use a 3rd party engine) so that Qt's regexp 
support this kind of features and follow some standard (whether it be 
Perl, JavaScript, POSIX, ...).

Regards,

Jasmin

--
 [ signature omitted ] 

Message 3 in thread

Jasmin Blanchette schrieb:
> so that Qt's regexp 
> support this kind of features and follow some standard (whether it be 
> Perl, JavaScript, POSIX, ...).

vim! ;-)

Martin, not explicitely looking for another editor war...

-- 
 [ signature omitted ] 

Message 4 in thread

Hi Jasmin,

thanks, I understand now.

Minimal quantifiers would be appreciated ;-)

Regards,

Bernd
-- 
 [ signature omitted ] 

Message 5 in thread

Bernd Strohhaecker schrieb:
> with setMinimal(true) and the string "abc" I expected the QRegExp 
> "^(.*)(.)?(.)?$" to set the submatches $1=a, $2=b and $3=c.

Jasmins competent and authorative answer aside ;-) you could try if 
"^(.)*(.)?(.)?$" or "^(.)?(.)?(.)?$" accomplishes what you need. But 
that's just a "if you don't know how it works, try to find out" approach...

Martin

-- 
 [ signature omitted ] 

Message 6 in thread

Hi Martin,

also thanks. But this was only an example, the real regexp was more 
complicated and I only wondered why my first approach didn't work.

Especially because it workes fine in Perl - and also vbscript.regexp ;-)

FWIW, to realize this with greedy quantifier and leave exactly two chars 
for the later subexpressions you could also use a negative lookahead:

^((?:(?!..$).)*)(.)?(.)?$

cu, Bernd
-- 
 [ signature omitted ]