Solved

read in text file (.NET source code) using Regular Expressions??

Posted on 2014-03-25
8
454 Views
Last Modified: 2014-03-31
I want to read in the attached text file (.NET source code) using Regular Expressions.

I want to read in the data assigned to all the variables in the Public Sub New() code.

Would Regular Expressions do the trick?
ClientNotifications.vb
0
Comment
Question by:HLRosenberger
  • 4
  • 3
8 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 total points
ID: 39954718
Regex's aren't a full replacement for a parser, but they'll work in many simple cases.

It's simplest to understand if you do it in stages. Firstly, I would capture the text between Public Sub New and End Sub. This pattern should do it:
@"(?s)Public Sub New\(\).*?End Sub"

Open in new window


Then you'd need to extract the variables and their values. This is a little trickier, but I got the following pattern working in a regex tester:
@"(?m)^\s*(\w+) = ((?:[^\n\r]*_(?:\r?\n)){1,}[^\n\r]*|[^\n\r]*(?=\r?\n))"

Open in new window

You should be able to play with it here: http://www.myregextester.com/?r=3b0aab88

Note I had some trouble with that regex tester site today. Sometimes I had to tell the page to reload (and resubmit my data) to get it to display correctly.
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39954792
This works in RegExBuddy:
@"(?m)^\s+(\w+) = ((?:.*& _\r\n)*.*$)"

Open in new window

It's not necessary, in the sample, to do 2 stages, as there are no "property = value" in the rest of the sample, only in the Public Sub New().

HTH,
Dan
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
ID: 39962671
thanks!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39962681
Glad I could help!
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 39962734
One more question - in my sample code, if the variable names never changed and I knew the names externally, how could I use reg expressions to extract the data for each variable, i.e., after the equal sign up until the end of the logical line?

I would pass the variable name to some function, and then have it return the data.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39962751
Instead of the first (\w+) use Reported_Color, for ex. Then the value for that variable would be in $1
0
 
LVL 1

Author Comment

by:HLRosenberger
ID: 39967141
thankls
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39967205
You're welcome!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 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