Solved

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

Posted on 2014-03-25
8
453 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 34

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 34

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 34

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 34

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

829 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