Solved

using Regex in C#

Posted on 2003-12-01
2
231 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
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!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

778 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