1

I am parsing a file which contains following packets:

[propertyID="123000"] { 
  fillColor : #f3f1ed;
  minSize : 5;
  lineWidth : 3;
}

To scan just this [propertyID="123000"] fragment I havre this QRegExp

QRegExp("^\b\[propertyID=\"c+\"\]\b");

but that does not work? Here I have example code to parse that file above:

QRegExp propertyIDExp= QRegExp("\\[propertyID=\".*\"]");
propertyIDExp.setMinimal(true);

QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
    QTextStream in(&inputFile);
    while (!in.atEnd())
    {
        QString line = in.readLine();

        // if does not catch if line is for instance
        // [propertyID="123000"] {
        if( line.contains(propertyIDExp) )
        {
            //.. further processing
        }
    }
    inputFile.close();
}
Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
  • 1
    `QRegExp("\[propertyID=\"\d+\"\]")` ?????????? what is `c` – vks Aug 19 '15 at 11:46
  • @vks c = character ? – Ralf Wickum Aug 19 '15 at 11:49
  • No its not.....use `\d` for `numbers` and `.` for any character – vks Aug 19 '15 at 11:50
  • 2
    I think you misunderstood the docs: *c - A character represents itself unless it has a special regexp meaning. e.g. c matches the character c.* – Wiktor Stribiżew Aug 19 '15 at 11:51
  • CSS does not have a regular syntax and cannot be parsed using a regex! The *reg* in *reg*ex means regular expressions - a way to specify regular syntax parsers. When the language doesn't have a regular syntax, you can't use a regex to parse it (duh). At best you're creating a parser that will work only for some very specific examples of CSS, and will reject most valid CSS making your users hate you with a passion. Such hacks are highly unprofessional. – Kuba hasn't forgotten Monica Aug 19 '15 at 13:23

2 Answers2

0

Use the following expression:

QRegExp("\\[propertyID=\"\\d+\"]");

See regex demo

In Qt regex, you need to escape regex special characters with double backslashes, and to match digits, you can use the shorthand class \d. Also, \b word boundary prevented your regex from matching since it cannot match between the string start and [ and between ] and a space (or use \B instead).

To match anything in between quotes, use a negated character class:

QRegExp("\\[propertyID=\"[^\"]*\"]");

See another demo

As an alternative, you can use lazy dot matching with the help of .* and QRegExp::setMinimal():

QRegExp rx("\\[propertyID=\".*\"]");
rx.setMinimal(true);

In Qt, . matches any character including a newline, so please be careful with this option.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • d is for decimal. What can I user for any string.. The Id may also contain strings like [propertyID="circle"] – Ralf Wickum Aug 19 '15 at 11:50
  • I updated the answer with `QRegExp("\\[propertyID=\"[^\"]*\"]");` suggestion. If you want to exclude results with empty string inside the double quotes, use `+` instead of `*`. – Wiktor Stribiżew Aug 19 '15 at 11:52
  • If you need to only allow English characters inside the quotes, use `QRegExp("\\[propertyID=\"[A-Za-z]*\"]");`. – Wiktor Stribiżew Aug 19 '15 at 12:31
  • QRegExp("\\[propertyID=\".*\"]") is not working for me. I have english chars, numerals and some special chars like '?' QRegExp("\\[propertyID=\"[^\"]*\"]"); neither. – Ralf Wickum Aug 20 '15 at 06:22
  • Could you please post all the relevant code (the lines that use the regexp), and add more real-life examples? I am surprised that `.*` with `setMinimal(true)` does not work for you. [See this demo](https://regex101.com/r/vC9jO0/3) showing it really matches anything inside the quotes. – Wiktor Stribiżew Aug 20 '15 at 06:40
  • 1
    Perhaps either one of those will work for you: 1) `QRegExp propertyIDExp("\\[propertyID=\".*\"\\]");`, 2) `QRegExp propertyIDExp("[[]propertyID\\s*=\\s*\".*\"[]]");`, 3) `if (propertyIDExp.indexIn(line) > -1) {...};` – Wiktor Stribiżew Aug 20 '15 at 07:44
0
QRegExp("\\[propertyID=\".+?\"\\]")

You can use ..It will match any character except newline.Also use +? to make it non greedy or it will stop at the last instance of " in the same line

vks
  • 67,027
  • 10
  • 91
  • 124