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

Posted on 2014-03-25
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?
Question by:HLRosenberger
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
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:

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.
LVL 35

Accepted Solution

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().


Author Closing Comment

ID: 39962671
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

LVL 35

Expert Comment

by:Dan Craciun
ID: 39962681
Glad I could help!

Author Comment

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.
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

Author Comment

ID: 39967141
LVL 35

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
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…

739 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