Is there a program (like Astyle) which can UN-split lines?
Example:
// These
void foo(
int one,
int two,
double three);
double a = b *
c;
// Becomes this
void foo(int one, int two, double three);
double a = b * c;
Using any decent IDE with regex search replace capabilities you can match this: ,\s*\r\n\s*(.*)
And replace with this: , $1
This assumes \r\n is your newline match and $1 is how you print your first capture.
EDIT:
This is an easy change to your match code if you want to add in some other symbols: [,+*-]\s*\r\n\s*(.*)
One thing you'll have to look out for is commented lines like this:
// blah,
// blah*
// blah
Because the regex will pick them up as well.
Related
Say I have the following code
int func1(int a);
int func2(int a);
int func3(int a);
Using vim search and replace, I want to turn it into this
/* This function handles action 1*/
int func1(int a);
/* This function handles action 2 */
int func2(int a);
/* This function handles action 3 */
int func2(int a);
To search for each function is simple enough, I can simply do this
/int func.
But, I don't know how I can use the value of the . in the replace section. How can this be accomplished?
You would use what is colloquially called "capture groups" (though Vim doesn't really have a name for that mechanism).
In Vim, you define a capture group by wrapping what you want to capture with escaped parentheses:
:%s/foo\(what you want to capture\)bar/what you want to capturebaz
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and you use it with an escaped number in the range 1-9 corresponding to the position of the capture group in the pattern:
:%s/foo\(what you want to capture\)bar/\1baz
^^
See :h \(.
In this specific case:
:%s/int func\(.\)/\/* This function handles action \1 *\/\r\0
where we even use \0 which references the whole pattern.
I would like to force rematch in the following scenario - I'm trying to inverse match a qualifier after each element in a list. In other words I have:
"int a, b, c" =~ m{
(?(DEFINE)
(?<qualifs>\s*(?<qualif>\bint\b|\bfloat\b)\s*+(?{print $+{qualif} . "\n"}))
(?<decl>\s*(?!(?&qualif))(?<ident>[_a-zA-Z][_a-zA-Z0-9]*+)\s*(?{print $+{ident} . "\n"}))
(?<qualifsfacet>\s*\bint\b\s*+)
(?<declfacet>[_a-zA-Z][_a-zA-Z0-9]*+)
)
^((?&qualifsfacet)*+(?!(?&decl))
|(?&qualifs)*+(?&declfacet)
|((?&qualifsfacet)
(?&declfacet)(?<negdecl>\g{lastnegdecl}(,(?&decl)))
|(?&qualifs)*+(?&declfacet)(?<lastnegdecl>\g{negdecl})
(?# Here how to force it to retry last with new lastnegdecl)))$
}xxs;
And would like to have:
a
int
b
int
c
int
As output. Currently it's only this:
a
int
int
I think this might work if there is a way to tell the regex machine to retrigger a match for the new lastnegdecl that is being captured.
Well after some trying I finally figured it out (besides the obvious whitespace issues I had in my original post):
"int a, b, c" =~ m{
(?(DEFINE)
(?<qualifs>\s*+(?<qualif>\bint\b|\bfloat\b)\s*+(?{print $+{qualif} . "\n"}))
(?<decl>\s*+(?!(?&qualif))(?<ident>[_a-zA-Z][_a-zA-Z0-9]*+)\s*(?{print $+{ident} . "\n"}))
(?<qualifsfacet>\s*+(\bint\b|\bfloat\b)\s*+)
(?<declfacet>\s*+[_a-zA-Z][_a-zA-Z0-9]*+\s*+)
)
^((?&qualifsfacet)(?!(?&decl))
|(?&qualifs)*+(?&declfacet)
|(?<restoutter>(?=(?&qualifsfacet)(?&declfacet)
(?<rest>(?(<rest>)\g{rest}),(?&decl)))
((?&qualifs)(?&declfacet)\g{rest}|(?&restoutter)))
|(?&qualifsfacet)(?&declfacet)(,(?&declfacet))*+)$
}xxs;
Basically I'm doing a positive lookahead where decl are called with code but qualifs are not while also concatenating decl inside rest then doing a partial match with the qualifs and the rest and if it doesn't match it goes to do the same thing again. Maybe someone can explain it better but it works. The output of the program above is:
a
int
b
int
c
int
And there is a full match.
I want to find lines of code which declare functions with default arguments, such as:
int sum(int a, int b=10, int c=20);
I was thinking I would look for:
The first part of the matched pattern is exactly one left-parenthesis "("
The second part of string is one or more of any character excluding "="
exactly one equals-sign "="
a non-equal-sign
one or more characters except right parenthesis ")"
")"
The following is my attempt:
([^=]+=[^=][^)]+)
I would like to avoid matching condition-clauses for if-statements and while-loops.
For example,
int x = 5;
if (x = 10) {
x = 7;
}
Our regex should find functions with default arguments in any one of python, Java, or C++. Let us not assume that function declarations end with semi-colon, or begin with a data-type
Try this:
\([^)]*\w+\s+\w+\s*=[^),][^)]*\)
See live demo.
It looks for words chars (the param type), space(s), word chars (the param name), optional space(s), then an equals sign.
Add ".*" to each end to match the whole line.
Please check this one:
\(((?:\w+\s+[\w][\w\s=]*,*\s*){1,})\)
The above expression matches the parameter list and returns it as $1 (Group 1), in case it is needed for further processing.
demo here
Assuming the following sample inline comment:
/*
function newMethodName (int bar, String s) {
int i = 123;
}
s/\<foo\s*(/newMethodName (/g
*/
How would I match and replace such that it would, essentially, become uncommented. I got this far before giving up.
:%s/\/\*\(\_.\)*\*\//\1/
Solution
:%s/\/\*\(\_.*\)\*\//\1/
Your capture group ( ) is capturing one character or newline. Put the following * inside so that \1 replacement gets the whole string rather than just the first character.
I need to find a variable in a C program and need to convert its 1st letter to upper case. For example:
int sum;
sum = 50;
I need to find sum and I should convert it to Sum. How can I achieve this using regular expressions (find and replace)?
This can't be done with a regex. You need a C language parser for that, otherwise how would you know what is a variable, what is a keyword, what is a function name, what is a word inside a string or a comment...
.Net's Regex replace support what you want to do (if you can come up with the regular expression you need). The ReplaceCC function at the bottom is invoked to provide the replacement value.
static void Main(string[] args)
{
string sInput, sRegex;
// The string to search.
sInput = #"int sum;
sum = 1;";
// A very simple regular expression.
sRegex = "sum";
Regex r = new Regex(sRegex);
MyClass c = new MyClass();
// Assign the replace method to the MatchEvaluator delegate.
MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);
// Write out the original string.
Console.WriteLine(sInput);
// Replace matched characters using the delegate method.
sInput = r.Replace(sInput, myEvaluator);
// Write out the modified string.
Console.WriteLine(sInput);
}
public string ReplaceCC(Match m)
{
return m.Value[0].ToUpper () + m.Value.Substring (1);
}