Solved

How to add 2nd parameter to this function?

Posted on 2004-10-28
586 Views
Last Modified: 2012-05-05
Hoi all...
a while ago I asked here for help for a special regEx and I got a great answer. Now I want to extend the code I got but I am too dumb to handle it :(.

This is what I got:
---------------------------------------------
private static string CodeReplacer(Match match) {
      string content=CodeToHtml(match.Groups["content"].Value);
      if (match.Groups["url"].Success) {
........
      }
            throw new ArgumentException("Unrecognized code");
}

public static string CodeToHtml(string code) {
      return Regex.Replace(code, @"\[((url=(?<url>[^\]]+)(\](?<content>((?!\[/?url).)*)\[/url)?)|((?<tag>b|ul|li|center)\](?<content>((?!\[/?\k<tag>).)*)\[/\k<tag>)|img(?<position>left|center|right|l|c|r)?=(?<imageURL>[^\]]*))\]", new MatchEvaluator(CodeReplacer), RegexOptions.Singleline|RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
}
---------------------------------------------


What I want to do now:
I want to use a second parameter inside "CodeReplacer".
Would be something like this:
---------------------------------------------
private static string CodeReplacer(Match match,  cSomeClass inObject) {
      string content=CodeToHtml(match.Groups["content"].Value,  cSomeClass inObject);
      if (match.Groups["url"].Success) {
........
      }
            throw new ArgumentException("Unrecognized code");
}

public static string CodeToHtml(string code, cSomeClass inObject) {
      return Regex.Replace(code, @"\[((url=(?<url>[^\]]+)(\](?<content>((?!\[/?url).)*)\[/url)?)|((?<tag>b|ul|li|center)\](?<content>((?!\[/?\k<tag>).)*)\[/\k<tag>)|img(?<position>left|center|right|l|c|r)?=(?<imageURL>[^\]]*))\]", new MatchEvaluator(CodeReplacer), RegexOptions.Singleline|RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
}
---------------------------------------------


This does not work, because now "CodeReplacer" does not match "new MatchEvaluator(CodeReplacer)" anymore.

Does anybody understand the code above enough to help me? :)

Thank you, bye
Smoerble

0
Question by:Smoerble
    9 Comments
     
    LVL 8

    Expert Comment

    by:Razzie_
    Well, yes and no. I take it the MatchEvaluator constructor takes a CodeReplacer object as a parameter? There should be no error when adding a parameter to a method for that. Are you sure that is the error and could you maybe post the lines of code this produces the error?
    0
     
    LVL 13

    Expert Comment

    by:dungla
    Smoerble,

    Delegate MatchEvaluator takes only one Match as parameter and that returns a string, therefore you cannot add 2nd param to your CodeReplace function.

    Maybe you should try another way instead of add 2nd param to your function :-)
    0
     

    Author Comment

    by:Smoerble
    Hmmm... doesn't sound good...
    I don't really understand how these two methods work together, that's why I don't know how to find a different way to do what I want...

    Basicly I want to do this:
    the RegEx returns sometimes a match that contains an ID of something. If this is the case, I want to do something with the diea, but for this I need an object in this method, that's why I wanted to "give" this object via a 2nd parameter.
    0
     
    LVL 6

    Accepted Solution

    by:
    The generic approach goes like this:
    ---
    internal class CodeReplacerWrapper {

      private SomeClass inObject;

      internal CodeReplacerWrapper(SomeClass inObject) {
        this.inObject = inObject;  
      }

      internal static string CodeReplacer(Match match) {
         ...
         // Just use this.inObject here
      }
    }
    --

    And when using the regex, do something like this:

    Regex.Replace(code, @"regexgoeshere",  new MatchEvaluator(new CodeReplacerWrapper(inObject).CodeReplacer))

    There might be more optimized approaches available for certain circumstances, but that one should work quite nicely unless you have huge amount of Replacements to be made in which case the wrapper construction cost might become an issue.
    0
     

    Author Comment

    by:Smoerble
    Thanks for this.
    Unfortunatly I still have no idea how to re-write this to be able to give it more parameters.
    My understanding now is, that I cannot work with a wrapper like above... but if I can't how, would I recode this on my own?
    0
     
    LVL 6

    Expert Comment

    by:der_jth
    What do you mean you cannot? What is the problem with the wrapper approach given above?
    0
     

    Author Comment

    by:Smoerble
    I need to do something like this:
    -------------
      internal CodeReplacerWrapper(SomeClass inObject, SomeClass inAnotherObject) {
        this.inObject = inObject;  
      }
     internal static string CodeReplacer(Match match, SomeClass inAnotherObject) {
        if RegEx found something special, do something with inAnotherObject.
      }
    -------------

    From the posts above, my understanding now is, that you can't do this.
    0
     
    LVL 6

    Expert Comment

    by:der_jth
    Yes you can, but not just with the syntax you expect. The key point is that you cannot pass the second parameters to the method (as it would violate the delegate signature), but you can pass them to the wrapper object that the MatchEvaluator method belongs to. Please re-read my first post above and elaborate which part doesn't suit your plans. I believe we can work something out.
    0
     

    Author Comment

    by:Smoerble
    Oh, I think I see now what you mean... was blind and dumb.. so I will try to get your generic approach working and come back if it fails (as I maight be still dumb) :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now