Solved

using Regex in C#

Posted on 2003-12-01
2
230 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 38
Syntax error 9 45
C# Single Form 8 29
C# 2015 Downloading text file and preserving the line breaks 5 21
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 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

24 Experts available now in Live!

Get 1:1 Help Now