-1

I'm looking to split and remove a QString. There are several words in the QString, separated by one or more(×) of the following symbols:

A-frame×N
A-line×NA
A-OK×A
A-pole×N
A-Z test×h
A/C×N

output

N
NA
A
N
h
N

src Qt C++

QStringList verbs;
QFile inFile("example.txt");

    if ( inFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
        QString line, value;
        QTextStream stream( &inFile );
        stream.setCodec("UTF-8");

        for (int counter = 0; counter < 23; counter++) {
            line = stream.readLine();
            QRegExp sep("\w+$");
            verbs << line.remove(sep);

        }
    qDebug() << verbs;
    }

output

("3-D×AN", "4-F×N", "4-H'er×N", "4-H×A", "A battery×h", "a bon march×v", "a cappella×Av", "a capriccio×h", "a datu×h", "a fortiori×v", "a gogo×Av", "A horizon×h", "a la carte×Av", "a la king×A", "a la mode×A", "a la×P", "A level×h", "a posteriori×A", "a priori×A", "a punta d'arco×h", "a quo×h", "a rivederci×h", "A supply×h")
usef62
  • 11
  • 1
  • What have you tried? The site is here to help you improve your efforts, so you need to show you have made some. – Rob Anthony Aug 28 '17 at 07:07
  • You have been a member for over three years. That's plenty of time to have read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). You also should have [taken the SO tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly you should know how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 28 '17 at 07:10

2 Answers2

0

I tried to generalize the pattern as much as possible based on the provided samples:

[A-Z0-9][- \/]\b.+?\b×(?=\w{1,2}\b)

[A-Z0-9]  // 1 char within the defined ranges
[- \/]    // 1 char of defined options
\b.+?\b   // 1 or more chars of anything, lazily matched, surrounded
          // by word boundaries
×         // an ascii character
(?=       // looking ahead, assert the following matches
\w{1,2}\b // 1 or 2 word characters, then a word boundary
)         // end of look ahead
  • Flags: g
  • Steps: 237

Demo

If you run a replace-with-blank function using the pattern, you get what you wanted. If you want to capture the ending characters, it needs to be grouped (turn the look ahead into a capturing group (remove ?=)).

linden2015
  • 887
  • 7
  • 9
0

First you need to set locale with command QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); after that you should open the file .

When you are reading the string in line you should use regular expression as QRegExp(QString::fromUtf8("[×]"

Check below code :

int main()
{

QStringList verbs;
QFile inFile("example.txt");

if ( inFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) 
{
    QString line;
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QTextStream stream( &inFile );

    while(!stream.atEnd()) 
    {
        line = stream.readLine();
        int i = line.indexOf(QRegExp(QString::fromUtf8("[×]")),0);
        verbs << line.remove(0,i+1);

    }
qDebug() << verbs;
}


}

~

SKM
  • 1
  • 1
  • 4