473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 AccountIDRequired(ByVal target As
Object, ByVal e As RuleArgs) As Boolean
Return mAccountID.ToString.Length 0
End Function

I know the function name, and can easily match the first line:
Protected\sOverridable\sFunction\sAccountIDRequire d.*

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\sOverridable\sFunction\sAccountIDRequir ed.*End\sFunction)

If I know that it is always 3 lines (like the example) I could do:
Protected\sOverridable\sFunction\sAccountIDRequire d.*\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!

Apr 25 '07 #1
3 2265

Hvis du bruger dotnet kan du bruge RegexOptions.Multiline, jf:

Regex.Match(inputstreng, regex, RegexOptions.Multiline)
- ellers (?m:), fx:
(?m:etregulærtudtryk)
Vh. Morten
On 25 Apr., 21:42, Masa Ito <masa...@masaito.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 AccountIDRequired(ByVal target As
Object, ByVal e As RuleArgs) As Boolean
Return mAccountID.ToString.Length 0
End Function

I know the function name, and can easily match the first line:
Protected\sOverridable\sFunction\sAccountIDRequire d.*

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\sOverridable\sFunction\sAccountIDRequir ed.*End\sFunction)

If I know that it is always 3 lines (like the example) I could do:
Protected\sOverridable\sFunction\sAccountIDRequire d.*\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!

Apr 26 '07 #2
But the .* doesn't capture the newlines (/n). I want to capture

Use (.|\n)*
(Protected\sOverridable\sFunction\sAccountIDRequir ed.*End\sFunction)
this matches my test perfectly, but I need it to match as many lines as
necessary until the End Function.
Try something like:
(Protected\sOverridable\sFunction\sAccountIDRequir ed(.|\n)*End\sFunction)

Remember that this regex doesn't match if the function is defined like
(split lines with _):
Protected Overridable _
Function AccountIDRequired(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.ActiveDocument.ProjectItem.FileCodeModel.CodeE lements
traverse(ce)
Next
End Sub

Sub traverse(ByVal ce As CodeElement)
If ce.Kind = vsCMElement.vsCMElementFunction Then
If ce.Name = "AccountIDRequired" Then
Dim cme As CodeFunction = CType(ce, CodeFunction)

MsgBox(cme.StartPoint.CreateEditPoint.GetText(cme. EndPoint.CreateEditPoint))
' 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 AccountIDRequired(ByVal target As
Just out of curiosity, what do you use <Descriptionattribute at method
for? It has practical meaning only for properties and assemblies.
Properties with <Descriptionattribute 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
Apr 26 '07 #3
Peter Macej <pe***@helixoft.comwrote in
news:uy**************@TK2MSFTNGP06.phx.gbl:
>But the .* doesn't capture the newlines (/n). I want to capture

Use (.|\n)*
>(Protected\sOverridable\sFunction\sAccountIDRequi red.*End\sFunction)
this matches my test perfectly, but I need it to match as many lines
as necessary until the End Function.

Try something like:
(Protected\sOverridable\sFunction\sAccountIDRequir ed(.|\n)*End\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 AccountIDRequired(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.ActiveDocument.ProjectItem.FileCodeModel.CodeE lements
traverse(ce)
Next
End Sub

Sub traverse(ByVal ce As CodeElement)
If ce.Kind = vsCMElement.vsCMElementFunction Then
If ce.Name = "AccountIDRequired" Then
Dim cme As CodeFunction = CType(ce, CodeFunction)

MsgBox(cme.StartPoint.CreateEditPoint.GetText(cme. EndPoint.CreateEditPo
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 AccountIDRequired(ByVal target As
Just out of curiosity, what do you use <Descriptionattribute at
method for? It has practical meaning only for properties and
assemblies. Properties with <Descriptionattribute 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
Apr 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonas Galvez | last post by:
Hi, I'm trying to parse some binary data with regexes. It works well in the latest Python build, but I need to run this on Python 1.5.2. The binary data has a pattern like this: ...
7
by: Dan Stromberg | last post by:
I'm working on writing a program that will synchronize one database with another. For the source database, we can just use the python sybase API; that's nice and normal. For the target...
14
by: daveyand | last post by:
Hey guys, I've spent abit of time searching th web and also this group and cant seem to find a solution to my issue. Basically i want to get the current url, and then replace http:// with...
2
by: Allen | last post by:
In regex, ^ and $ shoudl match start/end of a line when the 'm' /multiline modifier is set -- however I just spent the better part of the day trying to figure out why it wasn't working as expected....
0
by: aarbit | last post by:
I've got these 3 lines in a string: abc def ghi Then, I take that string and run it with this: If Regex.IsMatch(myString, "^def$", RegexOptions.Multiline) Then outString = "Matches"
3
by: ommail | last post by:
Hi! I have a simple regural expression: \w+$ and text to match: abc dddd ggg hhh
13
by: brad | last post by:
Still learning C++. I'm writing some regex using boost. It works great. Only thing is... this code seems slow to me compared to equivelent Perl and Python. I'm sure I'm doing something incorrect....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.