c# - Use RegEx or Macro etc. to process / generate text..Macro...Script etc -
i trying improve / automate repetitive programming task.
i have bunch of files (hundreds) field declarations this: (this 1 has 66 declarations alone)
string _id = string.empty; string _itemid = string.empty; string _upc = string.empty; int? _unitid; ...etc...
i have generate code each line this: (first line of course)
/// <summary> /// allows modification default value /// </summary> /// <param name="id">blah blah</param> /// <returns>builder object allow fluent method chaining</returns> public itembuilder withid( string id ) { this._id = id; return this; }
everything boiler plate , there 2 values parsed variables regex macro.
the type declaration such string, int? bool, datetime etc. 1 can plopped boilerplate code is.
next var name read such _id, _itemid, _upc etc. difference though manipulations need done.
- _id becomes id , prefixed produce withid.
- _id becomes id , plopped 2 spots.
- _id plopped 1 spot.
each line follow pattern.
in c# writing snippet problem comes in getting each line generate.
/// <summary> /// allows modification default value /// </summary> /// <param name="$parmvarname$">used assign...to built with.</param> /// <returns>builder object allow fluent method chaining</returns> public $builderobject$ with$methodname$( $parmtype$ $parmvarname$ ) { this._$parmvarname$ = $parmvarname$; return this; }
i thinking copy declarations (66 in example above) text editor , run "macro" against generating 66 methods can cut , paste code file.
so if there way in visual studio, edit plus plus, linqpad, etc. create template, process field declarations, , stuff template values each line interested in learning....
please understand i'm not asking homework [but won't stop :) ] educate me on topic / capability need learn about.
thanks
you can use following match:
^([^\s]+)\s+_([^\s;]+)?.*
and extract/replace with:
public itembuilder with$2( $1 $2 )\n{\n\tthis._$2 = $2;\n\treturn this;\n}
see demo
Comments
Post a Comment