So, I'm writting app in Qt and I need to split a string into operators and variables like this:
a&&b||!c --> vars ["a", "b", "c"] and operators ["&&", "||", "!"].
I wrote this:
to get list of vars:
QRegExp rx("[&|!]+");
QStringList vars = exp.split(rx, QString::SkipEmptyParts);
for operators, I tried this one:
QRegExp oprx("[a-z]");
QStringList operators = exp.split(oprx, QString::SkipEmptyParts);
but it gives last operand like "||!".Can you help me with a pattern for spliting operators.
Related
I'd like to find all occurrences of strings in a long string which are placed between a set of two specific strings.
For eg.
String s = "abcdText('hello')abcd efghText('world')";
The regex pattern of strings would be Text(' and next ' and the results should be the List of strings enclosed between the pattern. Hence the expected output should be:
[hello, world]
After some searches, I found this. This explains my use case but it is in PHP and only meant to find digits.
You can try this way :
String myString = "abcdText('hello')abcd efghText('world')";
RegExp exp = RegExp(r"\('(.*?)'\)");
List<String> _list =[];
for (var m in exp.allMatches(myString)) {
_list.add(m[1].toString());
}
print(_list);
EDIT
String myString = "abcdText('hello')abcd efghText('world')";
RegExp exp = RegExp(r"Text\('(.*?)'\)");
List<String> _list =[];
for (var m in exp.allMatches(myString)) {
_list.add(m[1].toString());
}
print(_list);
I have the QString line like this "567\n1.23456 2.34567\n1.23456 2.34"
And I want only "whole" float numbers only between \n characters.
I need QStringList after split() that contains only this float numbers. QString::split() can use RegEx so maybe I can use som regex here.
i tried QStringList myList = QString("56\n1.12345 2.34567\n1.23456 2.34").split('\n') that returns me ["1.2345 2.34567"] so i need split this again to ["1.23456"] and ["2.34567"]
The Qt documentation for QString::split has your answer
QString str;
QStringList list;
str = "Some text\n\twith strange whitespace.";
list = str.split(QRegularExpression("\\s+"));
// list: [ "Some", "text", "with", "strange", "whitespace." ]
this regex \d+(\.\d+)? will give you any float/int number!
You should split at QRegularExpression("\\s+"). \s means whitespace (which includes both =space and \n=newline), + means one or more, and you need to escape the backslash.
I'm in search for an easy and foolproof way to convert an arbitrary QStringList to a single QString and back.
QStringList fruits;
fruits << "Banana", "Apple", "Orange";
QString packedFruits = pack(fruits);
QStringList unpackFruits = unpack(packedFruits);
// Should be true
// fruits == unpackFruits;
What might be the easiest solution for this kind of problem?
From QStringList to QString - QStringList::join:
Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).
QString pack(QStringList const& list)
{
return list.join(reserved_separator);
}
From QString to QStringList - QString::split:
Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.
QStringList unpack(QString const& string)
{
return string.split(reserved_separator);
}
Previous answers mentioned QString::split and QStringList::join which is the correct way, but if the separator you choose is included in any of the strings it will break your conversion.
You must prevent strings in the list to contain your separator with one of the following techniques:
Throw an error before QStringList::join if any string includes the separator
Ensure they can not contain the separator (for example storing the string with its QByteArray::toHex(myString.toLatin1()) representation, then using a separator that has character(s) outside of the range 0..9 and a..f. Then convert back with QString::fromLatin1(QByteArray::fromHex(myHexString)) afterward
Use any separator regardless if the strings contain it, but implement an escape logic for it before the join(), and an un-escape logic after the split(), so that the separator is never present in any of the strings at the time of join, but all instances of it will be restored.
Use QStringList::join() :
QStringList strList;
strList << "Banana" << "Apple" << "Orange" ;
QString str = strList.join(""); // str = "BananaAppleOrange";
str = strList.join(","); // str = "Banana,Apple,Orange";
I have some text like this:
"1.801908\t20.439980\t\r\n25.822865\t20.439305\t\r\n26.113739\t4.069647\t\r\n1.800252\t4.301690\t\r\n"
I want to split this text by lines and then by tabs. I am using QString split() function and QRegExp to do that in this way:
QStringList rows = text.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
QStringList cols = rows.at(0).split(QRegExp("[ \t]"), QString::SkipEmptyParts);
But what I got in cols is just one item:
"1.801908\920.439980\9"
As I understand, the first split replaced all the \t characters with \9. But I don't understand why and how to fix that. Any explanation?
In my project I need to determine occurrence of the string in the list of strings. The duplicates in the list are not allowed, and the order is irrelevant.
Please help me choose the best Qt container for the string search.
If you want a list of strings, Qt provides the QStringList class.
Once all strings are added, you can call the removeDuplicates function to satisfy your requirement of no duplicates.
To search for a string, call the filter function, which returns a list of strings containing the string, or regular expression passed to the function.
Here's an example, adapted from the Qt documentation: -
// create the list and add strings
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
// Oops...added the same name
list << "John Doe";
// remove any duplicates
list.removeDuplicates();
// search for any strings containing "Bill"
QStringList result;
result = list.filter("Bill");
result is a QStringList containing "Bill Murray" and "Bill Clinton"
If you just want to know if a string is in the list, use the contains function
bool bFound = list.contains("Bill Murray");
Found will return true.