Can I extract two words from a sentence using a regular expression?

58 views Asked by At

I want to use a regular expression to extract two words from a sentence: Mobile and Application.

It should only be extracted when there are two words.

For example,

abcd Mobile defe Application is also true.

Mobile Application asdbfdvdsc is also true.

Application asdbdfbdfb Mobile is also true.

Mobile, Application Regardless of the order/ no matter how many words there are between two words/ can I extract two words from a sentence using a regular expression?

1

There are 1 answers

0
Filipe Pavão Drumond On

It should be

(Mobile)(.*)(Application)|(Application)(.*)(Mobile)

Use the QString.replace(const QRegularExpression &re, const QString &after) to extract the undesired words from the text:

#include <QRegularExpression>

QString text = 
"abcd Mobile defe Application is also true.\n
Mobile Application asdbfdvdsc is also true\n
Application asdbdfbdfb Mobile is also true.";

text.replace(QRegularExpression("(Mobile)(.*)(Application)|(Application)(.*)(Mobile)"), "\\2");