Solved

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

Posted on 2014-03-25
8
458 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
[X]
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
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

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!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

691 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