Solved

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

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

Expert Comment

by:Dan Craciun
ID: 39962681
Glad I could help!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now