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
The \b metacharacter matches at the beginning or end of a word.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.
Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
You want a lazy match:
/(?<={).+?(?=public)/s
See also: What is the difference between .*? and .* regular expressions?
(which I also answered, as it seems)
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
I think you need this:
(?s)(?<={).+?(?=public)
its like the answer posted by Bohemian but its lazy, so it matches what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With