简体   繁体   English

如何使用QRegExp从QString获取子字符串?

[英]How to get substring from a QString using QRegExp?

I have recently started to work on QT. 我最近开始从事QT工作。 I have some operations to do with QString. 我有一些与QString有关的操作。 I need to get the sub string of the main string using regex matching the file path in the string. 我需要使用正则表达式匹配字符串中的文件路径来获取主字符串的子字符串。

i want something like following : 我想要以下内容:

QString str=" My file is strored on C:\My Folder\Files\Test.txt , and i need to move it."

QRegExp rx("([a-zA-Z]:\\(?:[^\\:]+\\)*((?:[^:\\]+)\.\w+))"); //matches file path

if(str.contains(rx))
{
      Qstring path=str.substring(rx);
      QMessageBox msgBox;
      msgBox.setText(path); // where path is "C:\My Folder\Files\Test.txt" .
      msgBox.exec();
}

I have searched for the function which returns the sub string , but till now i haven't found it. 我已经搜索了返回子字符串的函数,但是直到现在我还没有找到它。 Some functions like left(), right(), mid() are does some alike task but not exact. 诸如left(), right(), mid()类的某些功能执行某些相似的任务,但并不精确。 So please can anyone tell me how can i do this it. 所以请任何人告诉我我该怎么做。

I suppose you are looking for QRegExp::capturedTexts() . 我想您正在寻找QRegExp::capturedTexts() An example from the official documantation : 官方文件中的一个例子:

QRegExp rx("(\\d+)(\\s*)(cm|inch(es)?)");
int pos = rx.indexIn("Length: 36 inches");
QStringList list = rx.capturedTexts();
// list is now ("36 inches", "36", " ", "inches", "es")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM