What is wrong with :
/(?<={).+(?=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 only this text :
private:
double gram;
double Kilogram;
double Tonnes;
void SetGram(double);
void SetKiloGram(double);
void SetTonnes(double);
matching end
You want a lazy match:
/(?<={).+?(?=public)/s
See also: What is the difference between .*? and .* regular expressions?
(which I also answered, as it seems)
I think you need this:
(?s)(?<={).+?(?=public)
its like the answer posted by Bohemian but its lazy, so it matches what you want.
You need the "dot matches newline" switch turned on, and a non-greedy (.*?
) match:
(?s)(?<={).+?(?=public)
Quoting from the regex bible, the (?s)
switch means:
Turn on "dot matches newline" for the remainder of the regular expression.
Note that the slashes around your regex have nothing to do with regex - that's a language thing (perl, javascript, etc) and irrelevant to the actual question
/s
flag, I think the problem is that the pattern matches until the last public
and not the first - Kobi 2012-04-04 18:13