Qt / QRegExp 正则表达式用于括号和新行或"r"?
Qt / QRegExp regular expression for brackets and new lines or " "?
我对使用Qt QRegExp捕获文本感到困惑。我想要一个抓取数据从PHP代码简单:
public function asr($x)
{
echo "index";
}
解释:
public function <can be more spaces or new lines...> asr($x) <can be more spaces or/and new lines or/and tabs...>
{
echo "index";
}
我想从PHP文件中抓取什么?
public function asr($x)
{
我现在是怎么被抓住的?
QRegExp reg("(public|private|protected|static|final)(.*)function(.*)[(](.*)[)](.*)[n](.*)[{]", Qt::CaseInsensitive);
解释:
当我尝试添加[{]
或({)
从PHP文件中抓取大括号时,它不会抓取数据:
结果:
public function asr($x)
:
public function asr($x) {
注:($x)
和{
之间可以用换行符n
或一些空格如s
。
这将作为())(n|s)
,但没有运气:(
使用示例代码解析结果:
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
// regex for function with new line
QRegExp reg("((.*)public|private|protected|static|final)( *)function(.*)[(](.*)", Qt::CaseInsensitive);
// capture to list
QStringList list = reg.capturedTexts();
qDebug() << list;
while ( !in.atEnd() )
{
QString line_read = in.readLine();
//qDebug() << line_read; // read lines
qDebug() << reg.exactMatch(line_read); // find matches
}
}
}
FINAL RESULTS:
false
false
false
false
false
false
false
false
true // means found on this line for QRegExp policies
false
false
false
false
true
false
false
false
false
true
false
false
false
false
true
false
false
false
false
false
false
false
必须在""
内使用\
作为反斜杠。
您也不必在[]
中转义(){
QRegExp function("(public|private|protected|static|final)( *)function(.*)[(](.*)[)](.*)[{]", Qt::CaseInsensitive);
auto b = function.exactMatch( "public function ($d) n {" );
b = function.exactMatch( "public function ($d){" );
适合我。空格由(.*)
我知道什么是问题,逐行阅读而不是文件中的所有行,然后应该替换文件中的文本。我不应该使用in.readLine()
读取文件,所以我在这行找到{
,但不存在,因为花括号{
在下一行!
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- WriteConsoleOutputCharacterW - 我的新行在哪里?
- 为什么当测试用例数为 1 时,我的程序不输入而是打印新行?
- 如何运行 while 循环直到到达新行?
- 从文本文件读取时,如何知道是否已到达新行?
- 句子转换器在存在新行的情况下不起作用
- Qt / QRegExp 正则表达式用于括号和新行或"r"?