Regex: match until the first occurrence of a word

What's wrong:

/(?<={).+(?=public)/s

      

full text

class WeightConvertor {

private:
    double gram;
    double Kilogram;
    double Tonnes;
    void SetGram(double);
    void SetKiloGram(double);
    void SetTonnes(double);
matching end

public:
    WeightConvertor();
    WeightConvertor(double, double, double);
    ~WeightConvertor();
    void SetWeight(double, double, double);
    void GetWeight(double&, double& ,double&);
    void PrintWeight();
    double TotalWeightInGram();

public:

};

      

how can i match just this text:

private:
    double gram;
    double Kilogram;
    double Tonnes;
    void SetGram(double);
    void SetKiloGram(double);
    void SetTonnes(double);
matching end

      

+3


source to share


3 answers


You want a lazy match:

/(?<={).+?(?=public)/s

      



See also: What's the Difference. *? and. * regular expressions?
(to which I also answered as it seems)

+8


source


I think you need this:

(?s)(?<={).+?(?=public)

      



its like the answer posted by bohemian but its lazy so it matches what you want.

+1


source


You need to enable the "dot matches newline" radio button, not greedy ( .*?

) match:

(?s)(?<={).+?(?=public)

      

Output from regex bible , switch (?s)

means:

Include "dot match newline" for the rest of the regular expression.

Note that the slashes around your regex have nothing to do with the regex - this is a language thing (perl, javascript, etc.) and not relevant to the real question

0


source







All Articles