1

I am trying to split QString based on 19 characters per group.

Here is the string:

+1.838212011719E+04-1.779050827026E+00 3.725290298462E-09 0.000000000000E+00

I wish to split it into:

+1.838212011719E+04
-1.779050827026E+00
3.725290298462E-09
0.000000000000E+00

I have tryed using QRegularExpression, but I could not come up with a solution.

How to do this?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Archiac Coder
  • 156
  • 2
  • 13

2 Answers2

3

Solution

I would suggest you to use a loop instead of a regular expression.

Example

Here is an example I have prepared for you of how to implement this in C++:

bool splitString(const QString &str, int n, QStringList &list)
{
    if (n < 1)
        return false;

    QString tmp(str);

    list.clear();

    while (!tmp.isEmpty()) {
        list.append(tmp.left(n));
        tmp.remove(0, n);
    }

    return true;
}

Note: Optionally you can use QString::trimmed(), i.e. list.append(tmp.left(n).trimmed());, in order to get rid of the leading whitespace.

Result

Testing the example with your input:

QStringList list;

if (splitString("+1.838212011719E+04-1.779050827026E+00 3.725290298462E-09 0.000000000000E+00", 19, list))
    qDebug() << list;

produces the following results:

  • without QString::trimmed()

      ("+1.838212011719E+04", "-1.779050827026E+00", " 3.725290298462E-09", " 0.000000000000E+00")
    
  • with QString::trimmed()

      ("+1.838212011719E+04", "-1.779050827026E+00", "3.725290298462E-09", "0.000000000000E+00")
    
Community
  • 1
  • 1
scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 1
    Thank you for such a lucid explanation. Although regular expression solved my issue. But, I must say this gave me a much more insight of how to approach issues which can be controlled more manually by explicit coding. Nevertheless any reason for advising against the use of Regular Expression?? (Even in the documentation, its advised that regular expression is a powerful way to handle strings/text) hence i went for it – Archiac Coder Sep 15 '18 at 16:30
  • @KOUSHIKNAG, read [this](https://stackoverflow.com/a/52064840/5366641): _As expected, the QRegularExpression-based one is two orders of magnitude slower_ – scopchanov Sep 15 '18 at 16:32
  • @KOUSHIKNAG, furthermore, if you like the answer, consider upvoting it, once you have 15 or more reputation. – scopchanov Sep 15 '18 at 16:38
  • @KOUSHIKNAG, to give you one more reason supporting the method I propose: it works for any length of the input string. Try the proposed regular expression with a shorter or a longer string and see what happens. – scopchanov Sep 15 '18 at 16:45
  • 1
    Surely, the knowledge is worth 2 upvotes! I got your points! it's bang on! Regex has its flaws.I appreciate your efforts! :) – Archiac Coder Sep 15 '18 at 17:31
1

Use this regular expression:

^(.{19})(.{19})(.{19})(.{19})

I would also recommend using a tool like RegEx101. Give it a try ans see what happens.

enter image description here

scopchanov
  • 7,966
  • 10
  • 40
  • 68
help-info.de
  • 6,695
  • 16
  • 39
  • 41
  • Why you not add "$" to end of regexp? Maybe something like this can be right: /^\s*(.{19})(.{19})(.{19})(.{19})\s*$/ – Deep Sep 15 '18 at 13:28
  • Thanks, help-info.de. It worked for me. I had to tweak it a bit due to the presence of spaces and necessary wraps. I highly appreciate your effort. can you please tell me if we can split QBytearray based on any such condition? The split function of QBytearray works only for delimiters. But mine is a non delimited file. else i would have prefered to read into QBytearray. – Archiac Coder Sep 15 '18 at 14:58