Home > OS >  How to find the Postion of all matching substrings in a QStringList
How to find the Postion of all matching substrings in a QStringList

Time:07-16

i am looking for a way to find the cell position of all matching substrings in a QStringList.

The List is filled form a txt file looking like that:

10:36:50,590/2002/1800

10:36:50,621/2002/1801

10:36:50,652/2002/1802

10:36:50,684/2002/1803

10:36:50,715/2002/1803

10:36:50,746/2002/1803

10:36:50,777/2002/1803/0/0/Target_Hit
 
10:36:50,809/2002/1802

10:36:50,840/2002/1802

10:36:50,871/2002/1802

10:36:50,965/2000/1831/0/0/Target_Hit 

Each cell of the QStringList contain one line of the txt file. Now i want to find the absolut number of hits and postion of the cells conatining the substring "Target_Hit".

I tried to find the number of it like that: int number_of_hits = List.indexOf(QRegExp(".*\Target_Hit$)); but that returns a -1 so i guess the QRegExp is incorrect.

CodePudding user response:

You cannot use indexOf for this because it would only return you one index, and it seems that you are looking for multiple.

I would read the file line by line, check each line if it contains what you are looking for, and take note of that, effectively this pseudo code:

initialise a counter to zero.
initialise a position list to an empty list.
read each line
    check if the line contains the search string.
    if it does: append the position into a list and increment the counter.
    continue the next line.

Then, at the end, you could have all that you desired to have.

  •  Tags:  
  • c qt
  • Related