Filter text with a QRegExp - regex

I want to filter out ${...} with a QRegExp, such that it retuns a QStringList of all selected elements:
"${NAME}" << "${DAY}" << "${MEH}" << "${MEH}" << "${MEH}"
but somehow it doesn't work:
QString text = "Hello ${NAME} \
How is your ${DAY} so? \
Bye, ${MEH} ${MEH}\
${MEH}";
// Regex: /(\${.*})/g
QRegExp rx("/(\\${.*})/g");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(text, pos)) != -1) {
QString val = rx.cap(1);
list << val;
qDebug () << "Val: " << val;
pos += rx.matchedLength();
}
No output at all? What I am doing wrong?
Update:
QRegularExpression works, but only on a per-line and not per-entry level. Ideas?
QString text = "Hello ${NAME} \
How is your ${DAY} so? \
Bye, ${MEH} ${MEH}\
${MEH}";
QRegularExpression rx("(\\${.*})");
QRegularExpressionMatchIterator i = rx.globalMatch(text);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
qDebug() << "Value: " << match.captured(0);
}
Output:
Value: "${NAME} \t\t\tHow is your ${DAY} so? \t\t\tBye, ${MEH} ${MEH}\t\t\t${MEH}"

Change the regex to QRegularExpression rx("(\\${.*?})");
Your first attempt didn't work because QRegExp doesn't support global matching via /.../g, and in general, is not perl-compliant. In Qt5, you're better off using the much-improved QRegularExpression.
Your second attempt didn't work because your regex is too greedy. It's grabbing the opening curly brace { up to the last closing curly brace }.
If you add a question mark to the .* capture, you will turn off the greediness, and in this case it will only capture what you are looking for.
Be careful trying to scale this solution much farther though. In general, Regular expressions are not parsers.

Related

QString::replace doesnt replace

Problem
I am trying to convert a string to a C string. In doing so, I need to replace " with \". I use the following code to do this:
QString Converter::plain2C(const QString &in) {
QString out;
// Split input in each line
QStringList list = in.split(QChar('\n'));
for (int i = 0; list.length() > i; i++) { // Go throught each line of the input
QString line = list[i]; // Thats the line
line.replace(QChar('\\'), QLatin1String("\\\\")); // replace \ with \\
line.replace(QChar('"'), QLatin1String("\\\"")); // replace " with \"
// For printf()
if(escapePercent)
line.replace(QChar('%'), QLatin1String("%%"));
// If option "Split output into multiple lines" is active add a " to the output
if (multiLine)
out.append(QChar('"'));
// append the line to the output
out.append(line);
// append a "\n" to the output because we are at the end of a line
if (list.length() -1 > i)
out.append(QLatin1String("\\n"));
// If option "Split output into multiple lines" is active add a " and \n to the output
if (multiLine) {
out.append(QChar('"'));
out.append(QChar('\n'));
}
}
if (!multiLine) {
out.prepend(QChar('"'));
out.append(QChar('"'));
}
return out;
}
However, " is still there without a \ before.
Information
Qt Version 5.15.3
C++17
Edit
The application is used to enter a normal string copied from the Internet and get as a result a string that can be copied into a C/C++ program.
More code
void Converter::run()
{
if (_from != From::NotSupportet &&
_to != To::toInvalid) {
QString out;
// More code obove and below
else if (_from == From::Plain) {
switch (_to) {
case To::toCString:
out = plain2C(_in);
break;
// Emits the ready string which is applied direct to a QPlainTextEdit
emit htmlReady(out);
}
}
Edit 2
Added more comments to the code
It works now. The problem was the line above:
line.replace(QChar('\'), QLatin1String("\\\\")); // replace \ with \\
The problem was that the comment ended with 2 \. That somehow disabled the next line or something like that.
Anyway, this is the working code:
QString Converter::plain2C(const QString &in) {
QString out;
// Split input in each line
QStringList list = in.split(QChar('\n'));
for (int i = 0; list.length() > i; i++) { // Go throught each line of the input
QString line = list[i]; // Thats the line
line.replace(QChar('\\'), QLatin1String("\\\\")); // replace "\" with "\\"
line.replace(QChar('"'), QLatin1String("\\\"")); // replace " with \"
// For printf()
if(escapePercent)
line.replace(QChar('%'), QLatin1String("%%"));
// If option "Split output into multiple lines" is active add a " to the output
if (multiLine)
out.append(QChar('"'));
// append the line to the output
out.append(line);
// append a "\n" to the output because we are at the end of a line
if (list.length() -1 > i)
out.append(QLatin1String("\\n"));
// If option "Split output into multiple lines" is active add a " and \n to the output
if (multiLine) {
out.append(QChar('"'));
out.append(QChar('\n'));
}
}
if (!multiLine) {
out.prepend(QChar('"'));
out.append(QChar('"'));
}
return out;
}

Extract strings inside double quotes with QRegularExpression

I have a string like below:
on prepareFrame
go to frame 10
goToNetPage "http://www.apple.com"
goToNetPage "http://www.cnn.com"
etc..
end
I want to extract all the urls from this string by using QRegularExpression. I've already tried:
QRegularExpression regExp("goToNetPage \"\\w+\"");
QRegularExpressionMatchIterator i = regExp.globalMatch(handler);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString handler = match.captured(0);
}
But this not working.
You may use
QRegExp regExp("goToNetPage\\s*\"([^\"]+)");
QStringList MyList;
int pos = 0;
while ((pos = regExp.indexIn(handler, pos)) != -1) {
MyList << regExp.cap(1);
pos += regExp.matchedLength();
}
The pattern is
goToNetPage\s*"([^"]+)
It matches goToNetPage, 0 or more whitespace chars, " and then captures into Group 1 any 1+ chars other than " - the required value is accessed using regExp.cap(1).

How can use a variable as a regex pattern in QT frame work C++

I want to use a string variable as a Regex pattern. But code can not find matches:
QString pattern, m;
QRegularExpression re (pattern);
QRegularExpressionMatch match;
QRegularExpressionMatchIterator i;
void MainWindow::on_pushButton_clicked()
{
pattern = ui->lineEdit->text();
k = re.globalMatch(ui->lineEdit_2->text());
while (i.hasNext()) {
match = i.next();
m = m +" "+ match.captured(0);
}
ui->label>setText(n);
}
See comments below
QString pattern, m; // Construct an empty 'pattern'
QRegularExpression re (pattern); // Construct 're' from that empty pattern
QRegularExpressionMatch match;
QRegularExpressionMatchIterator i;
void MainWindow::on_pushButton_clicked()
{
pattern = ui->lineEdit->text(); // Change 'pattern' - won't have any
// effect on 're' of course.
k = re.globalMatch(ui->lineEdit_2->text()); // Use the original 're'
while (i.hasNext()) {
match = i.next();
m = m +" "+ match.captured(0);
}
ui->label>setText(n);
}
You have to construct re after you have changed pattern:
QString pattern = ui->lineEdit->text(); // Initialize pattern.
QRegularExpression re(pattern); // Construct 're' from 'pattern'
k = re.globalMatch ...

QString replace only first occurrence

Is there simple way of replacing only first occurrence of some substring by other substring in QString? It can be at any position.
You could try this:
QString str("this is a string"); // The initial string.
QString subStr("is"); // String to replace.
QString newStr("at"); // Replacement string.
str.replace(str.indexOf(subStr), subStr.size(), newStr);
Resulting string will be:
that at a string
There is no convenience method for the operation you wish to have. However, you can use the following two methods to build your custom operation:
int QString::indexOf(const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns the index position of the first occurrence of the string str in this string, searching forward from index position from. Returns -1 if str is not found.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
and
QString & QString::replace(int position, int n, const QString & after)
Replaces n characters beginning at index position with the string after and returns a reference to this string.
Note: If the specified position index is within the string, but position + n goes outside the strings range, then n will be adjusted to stop at the end of the string.
Now, putting all that into practice, you could write something as follows:
main.cpp
#include <QString>
#include <QDebug>
int main()
{
QString initialString = QLatin1String("foo bar baz");
QString fooString = QLatin1String("foo");
initialString.replace(initialString.indexOf(fooString),
fooString.size(), QLatin1String("stuff"));
qDebug() << initialString;
return 0;
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Output
"stuff bar baz"
This is pretty much the way QString::replace(QRegularExpression, ... does it. Since it's possible that literal backslashes could be part of replace pattern, those need to be captured differently. Note that actual replacement happens right-to-left to preserve leftward offset validity. It's possible to put this more compactly, but easier to debug in this form.
QRegularExpression regex = QRegularExpression(regex_pattern);
if (regex.isValid() and
(regex_pattern.length() > 0)) {
QRegularExpressionMatchIterator regex_iterator =
regex.globalMatch(target_text, Apply_Target_Offset,
QRegularExpression::PartialPreferCompleteMatch);
if (regex_iterator.hasNext()) {
// At least one found
QRegularExpressionMatch match = regex_iterator.next();
if (match.hasMatch() and (not match.hasPartialMatch())) {
// This is the first match, and it's complete
int match_begin = match.capturedStart();
int match_end = match.capturedEnd();
int match_length = match.capturedLength();
QStringList captured;
const int capture_groups_count = regex.captureCount() + 1;
for (int capture_group_idx = 0; capture_group_idx < capture_groups_count; ++capture_group_idx) {
captured.append(match.captured(capture_group_idx));
}
QString replace_pattern = Apply_Replace_Pattern->toPlainText();
QString replace_text = replace_pattern;
QList<QRegularExpressionMatch> replace_pattern_match_list;
QRegularExpression replace_pattern_regex = QRegularExpression("(?:\\\\\\\\)+|(?:\\\\(\\d+))");
if (replace_pattern_regex.isValid()) {
QRegularExpressionMatchIterator replace_pattern_regex_iterator =
replace_pattern_regex.globalMatch(replace_pattern);
while (replace_pattern_regex_iterator.hasNext()) {
QRegularExpressionMatch replace_pattern_match = replace_pattern_regex_iterator.next();
bool no_error;
replace_pattern_match.captured().right(1).toInt(&no_error);
// Only accept backreferences w/ numbers
if (no_error) replace_pattern_match_list.append(replace_pattern_match);
}
while (replace_pattern_match_list.count() > 0) {
QRegularExpressionMatch replace_pattern_match = replace_pattern_match_list.takeLast();
int cap_idx = replace_pattern_match.captured(1).toInt();
if (cap_idx < captured.count()) {
replace_text.replace(replace_pattern_match.capturedStart(),
(replace_pattern_match.capturedEnd() -
replace_pattern_match.capturedStart()),
captured[cap_idx]);
}
}
// Render '\' characters properly
replace_text.replace("\\\\", "\\");
}
target_text.replace(match_begin, (match_end - match_begin), replace_text);
}
}
}
//------------------------------------------------------------------
QString & replace_first(QString &io_haystack, const QString & sub_str, const QString & new_str)
{
io_haystack.replace(io_haystack.indexOf(sub_str), sub_str.size(), new_str);
return io_haystack;
} // replace_first
//------------------------------------------------------------------
QString & replace_first(QString &io_haystack, const QRegularExpression & sub_regx, const QString & new_str)
{
QRegularExpressionMatch match;
match = sub_regx.match(io_haystack);
if (match.hasMatch()) {
QString sub_str = match.captured(0);
io_haystack.replace(io_haystack.indexOf(sub_str), sub_str.size(), new_str);
}
return io_haystack;
} // replace_first

Remove '\n.\n' C++

If i have a string as such
"I am not here... \n..Hello\n.\n....Whats happening"
I want to replace the above string so:
"I am not here... \n..Hello\n. \n....Whats happening"
^ Space added
Just a bit of a background on what im doing. Im using sendmail in C++ and \n.\n is End Of Message Equivalent of sendmail. I just created a class that uses sendmail to send mails. but obviously if the user from the outsite gives sendmail that command i want it to be removed. Here is my message function just incase.:
//Operator to add to the message
void operator<<(string imessage){
if (imessage != ""){ message += imessage; }
}
How would i go about doing this. Thanks in advance :D
This is my last version :)
This code handles the case mentioned by #Greg Hewgill
string& format_text(string& str)
{
const string::size_type dot_offset = 2;
string::size_type found_at_start = str.find("\n.\n"),
found_at = str.find("\n.\n");
if(found_at_start != string::npos)
str.insert(0, " ");
while(found_at != string::npos)
{
str.insert(found_at+dot_offset+1, " ");
found_at = str.find("\n.\n", found_at+1);
}
return str;
}
int main()
{
string text = ".\nn\n.\nn";
std::cout << format_text(text);
}
Look up String.find and String.replace
For example (not tested)
string endOfMessage = "\n.\n";
string replacement = "\n. \n";
size_t position;
while (position = message.find(endOfMessage))
{
message.replace(position, endOfMessage.length(), replacement);
}
This is derived from Dan McG's answer so upvote him ;)
string endOfMessage = "\n.\n";
string replacement = "\n. \n";
size_t position;
while (position = message.find(endOfMessage, position) != message.npos)
{
message.replace(position, endOfMessage.length(), replacement);
position += replacement.length();
}
Boost has Boost.Regex (a regular expression module). Might be overkill if this is the only replacement you need to do.
Use std::search and the insert method of sequence containers such as string, deque, or whatever you use to store the message text.
typedef std::string::iterator SIter; // or whatever container you use
static const char *end_seq = "\n.\n";
for ( SIter tricky_begin = msg.begin();
tricky_begin = std::search( tricky_begin, msg.end(), end_seq, end_seq+3 ),
tricky_begin != msg.end(); ) {
tricky_begin = msg.insert( tricky_begin + 2, ' ' );
}