Regular Expression (PCRE) question

Don HO
2012-12-04
2012-12-15
  • Don HO
    Don HO
    2012-12-04

    I would search in Notepad++ a c++ class block like the following sample:


    class LinePPState {
    int state;
    public:
    LinePPState() : state(0) {
    }
    void StartSection(bool on) {
    level++;
    if (ValidLevel()) {
    if (on) {
    state &= ~maskLevel();
    ifTaken |= maskLevel();
    } else {
    state |= maskLevel();
    ifTaken &= ~maskLevel();
    }
    }
    }
    }

    And I have done a quite naive re so far:
    ^[\t ](class|struct)[\t ]+[\w]+[\s](:[\s]public[\s]+[\w]+[\s])?{[^}]*};?

    Obviously, the re above cannot cover the provided example.
    Is there a way in pcre to get all matched { and } in a block?

    Thank you in advanced,
    Don

     
  • Regular expressions define the same languages that can be recognized by a finite state machine, and it cannot match brackets because it cannot count brackets more than a finite predefined number (since the state is finite). To match brackets, a more powerful description, like a context-free grammar, is required. Thus, if PCRE really does regular expressions, it cannot match brackets in the general case.

     
  • Don HO
    Don HO
    2012-12-04

    Thank you François for you answer.
    Then I will use an ugly way to accomplish this task.

    Don

     
  • Don HO
    Don HO
    2012-12-15

    Thank you Adrian for your links.
    However, after giving some tries, it seems that PCRE (from boost) of Notepad++ doesn't support such variations.

    Don

     
    Last edit: Don HO 2012-12-15