Regex'ing code functions - multiline?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Masa Ito

    #1

    Regex'ing code functions - multiline?

    I am trying to capture the contents of a function with Regex. I am using
    Expresso to test (nice - thanks for the great tool UltraPico!). I can
    handle my own with single line regex's (I think).. I want to have a
    named capture of the entire 'contents' of specific functions.
    EG: Sample code
    <Description("{ 0} is a required field.")_
    Protected Overridable Function AccountIDRequir ed(ByVal target As
    Object, ByVal e As RuleArgs) As Boolean
    Return mAccountID.ToSt ring.Length 0
    End Function

    I know the function name, and can easily match the first line:
    Protected\sOver ridable\sFuncti on\sAccountIDRe quired.*

    But the .* doesn't capture the newlines (/n). I want to capture
    everything up to the End Function, so figured I could do something like:
    (Protected\sOve rridable\sFunct ion\sAccountIDR equired.*End\sF unction)

    If I know that it is always 3 lines (like the example) I could do:
    Protected\sOver ridable\sFuncti on\sAccountIDRe quired.*\n.*\n. *

    this matches my test perfectly, but I need it to match as many lines as
    necessary until the End Function.

    Can anyone help?

    TIA!

  • Maate

    #2
    Re: Regex'ing code functions - multiline?


    Hvis du bruger dotnet kan du bruge RegexOptions.Mu ltiline, jf:

    Regex.Match(inp utstreng, regex, RegexOptions.Mu ltiline)


    - ellers (?m:), fx:


    (?m:etregulærtu dtryk)


    Vh. Morten


    On 25 Apr., 21:42, Masa Ito <masa...@masait o.comwrote:
    I am trying to capture the contents of a function with Regex. I am using
    Expresso to test (nice - thanks for the great tool UltraPico!). I can
    handle my own with single line regex's (I think).. I want to have a
    named capture of the entire 'contents' of specific functions.
    EG: Sample code
    <Description("{ 0} is a required field.")_
    Protected Overridable Function AccountIDRequir ed(ByVal target As
    Object, ByVal e As RuleArgs) As Boolean
    Return mAccountID.ToSt ring.Length 0
    End Function
    >
    I know the function name, and can easily match the first line:
    Protected\sOver ridable\sFuncti on\sAccountIDRe quired.*
    >
    But the .* doesn't capture the newlines (/n). I want to capture
    everything up to the End Function, so figured I could do something like:
    (Protected\sOve rridable\sFunct ion\sAccountIDR equired.*End\sF unction)
    >
    If I know that it is always 3 lines (like the example) I could do:
    Protected\sOver ridable\sFuncti on\sAccountIDRe quired.*\n.*\n. *
    >
    this matches my test perfectly, but I need it to match as many lines as
    necessary until the End Function.
    >
    Can anyone help?
    >
    TIA!

    Comment

    • Peter Macej

      #3
      Re: Regex'ing code functions - multiline?

      But the .* doesn't capture the newlines (/n). I want to capture

      Use (.|\n)*
      (Protected\sOve rridable\sFunct ion\sAccountIDR equired.*End\sF unction)
      this matches my test perfectly, but I need it to match as many lines as
      necessary until the End Function.
      Try something like:
      (Protected\sOve rridable\sFunct ion\sAccountIDR equired(.|\n)*E nd\sFunction)

      Remember that this regex doesn't match if the function is defined like
      (split lines with _):
      Protected Overridable _
      Function AccountIDRequir ed(ByVal target As Object, ByVal e As RuleArgs)
      As Boolean

      If you need to parse the method inside VS, I would use VS automation
      (macro or add-in). Create following two macros and call test() macro.
      This will do what you want:
      Sub test()
      Dim ce As CodeElement
      For Each ce In
      DTE.ActiveDocum ent.ProjectItem .FileCodeModel. CodeElements
      traverse(ce)
      Next
      End Sub

      Sub traverse(ByVal ce As CodeElement)
      If ce.Kind = vsCMElement.vsC MElementFunctio n Then
      If ce.Name = "AccountIDRequi red" Then
      Dim cme As CodeFunction = CType(ce, CodeFunction)

      MsgBox(cme.Star tPoint.CreateEd itPoint.GetText (cme.EndPoint.C reateEditPoint) )
      ' use properties of cme to investigate the method further
      End If
      ElseIf ce.IsCodeType Then
      Dim tce As CodeType = CType(ce, CodeType)
      Dim subCe As CodeElement
      For Each subCe In tce.Members
      traverse(subCe)
      Next
      End If
      End Sub

      See http://www.helixoft.com/blog/archives/6 how to create macro.
      <Description("{ 0} is a required field.")_
      Protected Overridable Function AccountIDRequir ed(ByVal target As
      Just out of curiosity, what do you use <Descriptionatt ribute at method
      for? It has practical meaning only for properties and assemblies.
      Properties with <Descriptionatt ribute will show this description in
      Properties window. Not methods. To have IntelliSense tooltips and Object
      browser description, you need to use XML comments (<summary>, <param>,
      etc.) instead, see http://tinyurl.com/ywtomx

      Regards

      --
      Peter Macej
      Helixoft - http://www.helixoft.com
      VSdocman - Commenter and generator of class documentation for C#, VB
      ..NET and ASP .NET code

      Comment

      • Masa Ito

        #4
        Re: Regex'ing code functions - multiline?

        Peter Macej <peter@helixoft .comwrote in
        news:uyMmB39hHH A.3512@TK2MSFTN GP06.phx.gbl:
        >But the .* doesn't capture the newlines (/n). I want to capture
        >
        Use (.|\n)*
        >
        >(Protected\sOv erridable\sFunc tion\sAccountID Required.*End\s Function)
        >this matches my test perfectly, but I need it to match as many lines
        >as necessary until the End Function.
        >
        Try something like:
        (Protected\sOve rridable\sFunct ion\sAccountIDR equired(.|\n)*E nd\sFunctio
        n)
        Interesting - thanks - I will give this a try. I haven't seen that
        syntax before. I actually noticed the 'single line' option description
        in Expresso describes it as 'make . match \n as well as any other
        character' - which is exactly what I had wanted. For some reason, I had
        thought the opposite - that Singleline would not match line breaks - go
        figure!?
        Remember that this regex doesn't match if the function is defined like
        (split lines with _):
        Protected Overridable _
        Function AccountIDRequir ed(ByVal target As Object, ByVal e As
        RuleArgs) As Boolean
        >
        If you need to parse the method inside VS, I would use VS automation
        (macro or add-in). Create following two macros and call test() macro.
        This will do what you want:
        Sub test()
        Dim ce As CodeElement
        For Each ce In
        DTE.ActiveDocum ent.ProjectItem .FileCodeModel. CodeElements
        traverse(ce)
        Next
        End Sub
        >
        Sub traverse(ByVal ce As CodeElement)
        If ce.Kind = vsCMElement.vsC MElementFunctio n Then
        If ce.Name = "AccountIDRequi red" Then
        Dim cme As CodeFunction = CType(ce, CodeFunction)
        >
        MsgBox(cme.Star tPoint.CreateEd itPoint.GetText (cme.EndPoint.C reateEditPo
        int))
        ' use properties of cme to investigate the method
        further
        End If
        ElseIf ce.IsCodeType Then
        Dim tce As CodeType = CType(ce, CodeType)
        Dim subCe As CodeElement
        For Each subCe In tce.Members
        traverse(subCe)
        Next
        End If
        End Sub
        See http://www.helixoft.com/blog/archives/6 how to create macro.
        That is a great idea - for some reason I hadn't even thought about
        macros/programming in vs.

        I ended up getting my code to do work great - I am upgrading a very large
        CSLA based app from 1.5 to 2.1, and this section of code updated
        thousands of validation rules for me ... :)

        <Description("{ 0} is a required field.")_
        Protected Overridable Function AccountIDRequir ed(ByVal target As
        Just out of curiosity, what do you use <Descriptionatt ribute at
        method for? It has practical meaning only for properties and
        assemblies. Properties with <Descriptionatt ribute will show this
        description in Properties window. Not methods. To have IntelliSense
        tooltips and Object browser description, you need to use XML comments
        (<summary>, <param>, etc.) instead, see http://tinyurl.com/ywtomx
        This is used in the validation/error text - when a rule is broken this
        text is displayed to the end user in the UI.

        Thanks a ton for your help and ideas!

        Masa

        Comment

        Working...