Take the 2-minute tour ×

I am new to stackoverflow (my first post) and regex. Currently i am working on a simple dirty app to replace baseclass properties with ctor injected fields. (cos i need to edit about 400 files)

It should find this:

ClassName(WiredObjectRegistry registry) : base(registry)

and replace with:

ClassName(IDependency paramName, ISecondDependency secondParam, ... )
  _fieldName = paramName;

so i need to replace the two old lines with three or more new lines.

basically i was thinking:

find this ->

className + ctorParams + zero or more whitespaces + newline + zero or more whitespaces + {

replace with ->

className + newCtorParams + newline + { my field assignments

i tried this regex for .net

className + ctorParam + @"\w*" + "\r|\n" + @"\w*" + @"\{"

which does not replace the "{" and the whitespaces correctly

the replaced file content looks like this:

      public CacheManager(ICallManager callManager, ITetraEventManager tetraEventManager, IConferenceManager conferenceManager, IAudioManager audioManager)
        _callManager = callManager;
_tetraEventManager = tetraEventManager;
_conferenceManager = conferenceManager;
_audioManager = audioManager;

can u please help me with this :-|


share|improve this question
When you type your question there is some information on the screen that tells you how to format your question, including how to insert code blocks. –  Mark Byers Mar 7 '10 at 12:14
What does the original file (before replacement) look like? –  Tim Pietzcker Mar 7 '10 at 12:26
We need to see the actual original text (the relevant portion, that is) and what it should look like after the replacement. Showing us incorrect results and how you got them doesn't help us. –  Alan Moore Mar 7 '10 at 13:53
add comment

1 Answer

If you're translating


into regex as


then you're making several errors.

First, the character class for whitespace is \s. \w means "alphanumeric character".

Second, "\r|\n" will result in the alternation operator | separating the entire regex in two alternative parts (= "match either the regex before the | or the regex after the |"). In your case, you don't need this bit at all since \s will already match spaces, tabs and newlines. If you do want a regex that matches a Unix, Mac or DOS newline, use \r?\n?.

But, as the comments show, unless you show us what you really want to do, we can't help you further.

share|improve this answer
add comment

Your Answer


