tags:

views:

83

answers:

1

Hi guys,

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 :-|

david

A: 

If you're translating

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

into regex as

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

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.

Tim Pietzcker