I have this 3 functions, looking exactly like this in multiple files, and using Notepad++, Find in Files/Replace in Files:
FirstLine
{
//something here
}
SecondLine
{
//something here
}
ThirthLine
{
//something here
}
I want to remove the second function, the result should be like this:
FirstLine
{
//something here
}
ThirthLine
{
//something here
}
I have tried on regex101.com many times this is the last one i tried:
^.*(SecondLine).*\{.*\}.*$
just doesn't work, :(
This is what I ended up to use:
SecondLine[^\{]*\{[^\}]*\}\r*\n*
Notepad++ threw an Invalid regular expression error before I escaped braces. It seems closing braces should be escaped in a N++ regex to mean a literal } character.
Thank you all for the help!
Related
I'm checking a string which contains vehicle registration information against regular expressions for validity. I have several regular expression for each criteria I need. How can I validate the string against all my reg expressions without having to combine them into one expression or do something like this to determine if it's valid?
if( s_expGP.exactMatch(lineEdit->text()) ||
s_expGPNew.exactMatch(lineEdit->text()) ||
s_expPersonal.exactMatch(lineEdit->text()) ||
s_expGov.exactMatch(lineEdit->text()) )
{
//do stuff
}
The only option would be to create a single regular expression by combining s_expGP, s_expGPNew, s_expPersonal and the rest if that is possible, otherwise I don't think there could be any other way.
If you have a big number of regexp to test or if you may need to verify the string more than once. You can create a function like this
bool isValid(const QVector<QRegExp>& regExps, const QString& input)
{
for(QRegExp exp : regExps)
{
if(!exp.exactMatch(input))
return false;
}
return true;
}
Or use a static QVector like you have static regexp.
Is there a way to change format how netbeans 8.1 generate code? Instead of having
void setSomething1(bool something1)
{
something1_ = something1;
}
bool getSomething1() const
{
return something1_;
}
I would like to have this code
void setSomething1(bool something1) { something1_ = something1; }
bool getSomething1() const { return something1_; }
and also the set/get prefix could change or disappear
void setSomething1(bool something1) { something1_ = something1; }
bool something1() const { return something1_; }
netbeans only allow me to change braces position, same line or new line, but for this short functions i would like to compress it to one line only. With many setters and getters, the code starts to be confusing.
If netbeans itself cant do it, would it be possible via regular expression or some other workaroud?
Other (long body) functions will remain in the format. There it makes the code better to read.
retType name(args)
{
body;
}
This regex will satisfy the first requirement (Demo):
([\w ]+\s+[gs]et\w+\([\w ]*\)\s*(?:const)?)\s+\{\s+(.*)\s+\}
With $1 { $2 } as replacement
Then this one (note the trailing space):
get(\w+)\(
With $1 (trailing space aslo) as replacement
I have a very large source code(>10,000lines) to change, with some function with fn_i_dont_want, I don't need to change, all other functions I must do some changes.So it's very hard to find such functions in such a large source code.
For example:
int foo_i_dont_want()
{
fn_i_dont_want()
}
int foo_i_want1()
{
fn()
fn1()
}
int foo_i_want2()
{
fn()
fn1()
....
}
I want vim to search all function I want, foo_i_want1, foo_i_want2, the condition is I don't want function with function call fn_i_dont_want.
I've tried to use vim regex like
/{\_.\{-}fn_i_dont_want\_.\{-}}
or
/{\_.*fn_i_dont_want\_.*}
But everything fails, it gets worse when there is some function like this:
int foo_i_dont_want()
{
struct bar = {0,0};
fn_i_dont_want();
}
and vim searches for everything in the pair of bracket{}.
So can somebody help me?
Parsing a complex programming language with regular expression is bound to be incomplete and problematic. Instead:
either use the refactoring capabilities of a real IDE, or
if you have to do this in Vim use :substitute with the confirm flag, and manually accept / decline each match position after inspecting it
I have the following StringTemplate group
group RPInstr;
before(firstStat) ::= <<
<{<[beforeEnteringInstr(),firstStat]; anchor, separator="\n">}; anchor>
>>
beforeEnteringInstr() ::= "before();"
I am trying to make the first statement of a method to align with the instrumentation code ("before();").
However, what I am getting right now is something like this:
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
What is the proper way to indent before(); and the next statement so I get the following?
public int method() {
before();
System.out.println("testing");
System.out.println("testing again");
}
Thanks in advance.
PS: the options that I am using in my grammar are the following
options {output=template; rewrite=true; backtrack=true; memoize=true;}
the expr in before(firstStat) is not indented. No reason for ST to indent then.
I am using QRegExp and tries to find whether a QString is containing some pattern. There is no compiling error, but no match is identified at runtime where identification should normally happen. I tested the regexp in Python shell and match occurs with Python. i checked upon Qt doc that syntax is the same for the ergexp I am using. Here is code sample
bool Thing::isConstraint(const QString &cstr_)
{
QRegExp lB1("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)$");
QRegExp lB2("^PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp lB3("^PARAM(\d+)>(\d+\.?\d*|\d*\.\d+)$");
QRegExp lB4("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)$");
QRegExp uB5("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)$");
QRegExp uB6("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)$");
QRegExp uB7("^PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp uB8("^PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB9("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB10("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)>=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB11("^(\d+\.?\d*|\d*\.\d+)>=PARAM(\d+)>(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB12("^(\d+\.?\d*|\d*\.\d+)>PARAM(\d+)>(\\d+\.?\d*|\d*\.\d+)$");
QRegExp luB13("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB14("^(\d+\.?\d*|\d*\.\d+)<=PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB15("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)<=(\d+\.?\d*|\d*\.\d+)$");
QRegExp luB16("^(\d+\.?\d*|\d*\.\d+)<PARAM(\d+)<(\d+\.?\d*|\d*\.\d+)$");
int pos_=0;
if((pos_ = lB1.indexIn(cstr_)) != -1)
{
m_func->setLowerBound((lB1.cap(2)).toInt(),(lB1.cap(1)).toDouble());
return true;
}
else if((pos_ = lB2.indexIn(cstr_)) != -1)
{
m_func->setLowerBound((lB2.cap(1)).toInt(),(lB2.cap(2)).toDouble());
return true;
}
/*
...
*/
return false;
}
This method is called in this other method:
void Thing::setConstraints(QStringList &constraints_)
{
if(!m_func)
return;
for(int j=0;j<constraints_.size();j++)
{
if(isConstraint(constraints_.at(j)))
{
constraints_.removeAt(j);
}
}
m_func->setConstraints(constraints_);
}
In VS2010 Watch, error for lB1.indexIn(cstr_) is: Error: argument list does not match a function .
Second, I would like the isConstraint() method to begin with this check and replace for whitespaces:
QRegExp wsp ("\s+");
cstr_.replace(wsp,"");
how to proceed avoiding const_cast ??
Thanks and regards.
edit ---------
needed to double backslash in C++ - different from Python. Tks!
I think you asked two questions, so I'll try to answer them:
1) Your regular expressions are most likely not passing because you need to escape your backslashes so that C++ doesn't mess up your strings. For example:
QRegExp lB1("^(\\d+\\.?\\d*|\\d*\\.\\d+)<=PARAM(\\d+)$");
2) To avoid using const_cast you can either change your function signature to this:
bool Thing::isConstraint( QString cstr_)
or make a copy of the cstr_ object and operate on the copy instead.
As a side note, you may want to take a look at the QRegExp::exactMatch() function which obviates the need to use ^ and $ at the beginning and end of all of your expressions, and also has a bool return value which would make your if statements a little cleaner.