Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

.NET VB New Sub Replace Help? Not Quite Right

Last post 01-29-2008, 5:37 PM by Aussie Susan. 5 replies.
Sort Posts: Previous Next
  •  01-28-2008, 1:24 AM 38995

    .NET VB New Sub Replace Help? Not Quite Right

    Hi Guys,

     I am trying to generate some dynamic code. and need to replace the Public Sub New in vb.net

    Multiline and Case ignore is on and i am using .net regex engine.

    (PUBLIC|PRIVATE)\s+(SUB)\s+NEW([.\n]*|(END\s+SUB|END\s+FUNCTION))

    I Needs To Match

    Public Sub New()      *(With Or Without Brackets) - (Can be Either Public OR Private As Well)
                <Anything Here Could be 100lines long...>
    End Sub

    Any Ideas?
    Beachnerd
     

    Filed under: , , , ,
  •  01-28-2008, 5:33 PM 39018 in reply to 38995

    Re: .NET VB New Sub Replace Help? Not Quite Right

    What happens when you try your pattern against some real text? How is that different to what you expect?

    Just looking at the pattern I would make a couple of comments:

    1) the [.\n]* sub-pattern is greedy which means that it will match ALL of the following:

    Public Sub New()
       HelloString = "Hi"
    End Sub

    Private Function ErrorCode()
       HelloString = "Error"
    End Function

    2) You just look for 'sub' at the start but allow 'sub' or 'function' at the end

    3) While it is not wrong, you can make the last part of the pattern

        (END\s+(SUB|FUNCTION))

    4) This will find text in comments which is probably not a good place to look

    Actually, you can make this a bit tighter with something like

    ^\s*(public|private)\s+(sub|function)\s+NEW  ..other stuff here..  ^\s*END\s+\2$

    if you read the posting guidelines at http://regexadvice.com/forums/thread/37499.aspx you will see that we really require a number of things that you have not given us before we can really help. In particular, some real text, the regex (I'm assuming .NET) and a good description of what you are looking for and what you asre trying to do.

    Susan



     

  •  01-29-2008, 10:36 AM 39033 in reply to 39018

    Re: .NET VB New Sub Replace Help? Not Quite Right

    Aussie Susan:

    What happens when you try your pattern against some real text? How is that different to what you expect?

    Just looking at the pattern I would make a couple of comments:

    1) the [.\n]* sub-pattern is greedy which means that it will match ALL of the following:

    Public Sub New()
       HelloString = "Hi"
    End Sub

    Private Function ErrorCode()
       HelloString = "Error"
    End Function

     

    Actually Susan [.\n]* only matches two possible characters. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-29-2008, 2:38 PM 39054 in reply to 39033

    Re: .NET VB New Sub Replace Help? Not Quite Right

    Errr.. Ooops.....

    Didn't look closely enough and I read that as (.\n)* which I think the OP actually meant

    Susan 

  •  01-29-2008, 3:06 PM 39059 in reply to 39054

    Re: .NET VB New Sub Replace Help? Not Quite Right

    Aussie Susan:

    Errr.. Ooops.....

    Didn't look closely enough and I read that as (.\n)* which I think the OP actually meant

    Susan 

    Actually that still wouldn't be the same thing. Now you are just matching pairs of  characters, the last character and the newline after it. Where you going for (.|\n)*   I think what the OP wanted was the effect of singleline mode without using that option. I think the OP meant to use the character class they just used it incorrectly for their goal.  When I have some time later I am going to write up a blog article on the topic for a frame of reference.  This type of error is common and I was going to address another common error as well.

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  01-29-2008, 5:37 PM 39066 in reply to 39059

    Re: .NET VB New Sub Replace Help? Not Quite Right

    I really should learn not to look at this forum when I only have a few minutes before my train leaves on the morning, so that I can read what is really there and type what I really mean!

    Thank you for the correction

    Susan
     

View as RSS news feed in XML