Solved

using Regex in C#

Posted on 2003-12-01
2
233 Views
Last Modified: 2010-04-16
Hey guys...

Tryin to use Regex for the first time to write an interpreter for a really simple language, but i really dont know what i am doing with Regex, and am probably just wasting my time trying to do it by myself, so i figured yall could help..


lets say that i am trying to match the following phrase

.DATA MAX 100
.DATA(Keyword) MAX(Variable Name) 100(Value)

Regex PD = new Regex( "^.Data\s*(?<Name>\w*)\s*(?<Value>\w*)", RegexOptions.Compiled );

Match pMatch = PD.Match( ".DATA MAX 000h" );

if( pMatch.Success )
{
   string strName = PD.Groups[ "Name" ].Value;
   string strValue = PD.Groups[ "Value" ].Value;
}

will this strip every possible .DATA VAR_NAME VALUE string out there, no matter how many spaces are between the words, or tab characters, or comments (using #) after the value?

basically, i need that to be able to match

.DATA          MAX        100
.DATA MAX 100 #comment here


need help
thanks,
Xen
0
Comment
Question by:darkxenn
[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
2 Comments
 
LVL 3

Expert Comment

by:monkesdb
ID: 9853364
why don't you just test it??

i think it maybe should be

"^\.Data\s*(?<Name>\w*)\s*(?<Value>\w*)"

. is an 'any character' wildcard, if your string holds more than one of these  .DATA VAR_NAME VALUE  sets then just remover the ^

"\.Data\s*(?<Name>\w*)\s*(?<Value>\w*)"

HTH
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 250 total points
ID: 9854014
OK, this one will match it:
^.DATA\s*(?<Name>\w*)\s*(?<Value>\w*)

but it will not read any more characters after that, neither will search for end of text. It is important to match the case (DATA instead of Data).

Have a look here: http://www.regexlib.com/RETester.aspx. This is a good place to very quickly test your reg expressions
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 65
transaction in asp.net, sql server 6 47
c# - Best approach for objects in functions 3 28
Need a simple RegEx to search for two words 27 85
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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