Question

I would like to capture the string that is between 2 occurrences of word SAT and does not contain SAT. I have found this so far, which is not working

QLatin1String(".*SAT([^(SAT)]*)SAT.*")

I am afraid that [^(SAT)]* is not doing what i want, that is finding a string with not the word SAT. Or maybe the way i do the capture is not good:

QRegExp rx(pat);
int p = 0;
QString cap = QString::null;

if((p = rx.indexIn(str, p)) != -1) 
    cap = rx.cap(1).trimmed();
Was it helpful?

Solution

Try this one :

(?<=SAT)(.+?)(?=SAT)

What it actually does is look behind and forward for SAT.

If you want to test per string and use the whole string as the sample, then just use this modified version : ^(?<=SAT)(.+?)(?=SAT)$


Btw, just tried this one out (SAT([^(SAT)]+)SAT) and it seems to be working too. Here's a demo : http://regex101.com/r/fJ4gO5

OTHER TIPS

You can use lazy quantifier '?'.

(?<=SAT).*?(?=SAT)

Basically, you search for SAT, then you search as least symbols as possible until you find second SAT. Thus you will not have SAT in your captured string.

I have this one:

first, I capture whole string after first occurrence of SAT with this

const QString pat = QLatin1String("SAT(.*)");
QRegExp rx(pat);
int p = 0;
QString cap = QString::null;

if((p = rx.indexIn(str, p)) != -1) 
    cap = rx.cap(1).trimmed();

then, i look for the position of SAT in the result cap. And if i can indeed find this SAT again, i select the characters in cap before SAT :

QRegExp rxrev(QLatin1String("SAT"));
if((p = rxrev.indexIn(cap, 0)) != -1) 
   cap = cap.mid(0,p);

It has advantage that it only uses simplest QRegEx methods and works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top