Re: How do I parse this string into int fragments?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Cezary Nolewajka (c.nolewajka-no-sp-am-eh_at_no-sp-am-eh-mail.com)
Date: 02/04/04


Date: Wed, 4 Feb 2004 23:05:59 +0100

A good idea to use regular expressions for such parsing. Then you don't need to fiddle with the code too much when the syntax of the expression changes (for instance you add another module with "-" and another integer).

The code to parse your syntax would be:

   string strRxPattern = "(?<int1>\\d*)-(?<int2>\\d*)";
   string strToCheck = "1287103871-87450";
   Regex rx = new Regex (strRxPattern);

   if (rx.IsMatch (strToCheck))
   {
    Match mt = rx.Match (strToCheck);
    Console.WriteLine (string.Format ("{0}: {1}", "int1", mt.Groups ["int1"].Value));
    Console.WriteLine (string.Format ("{0}: {1}", "int2", mt.Groups ["int2"].Value));
   }

So, you end up with nicely split strings... :-) Have a good time with regexping! :-)

-- 
Cezary Nolewajka
mailto:c.nolewajka-no-sp-am-eh@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"Top Gun" <nfr@nospam.com> wrote in message news:OeucqW26DHA.1672@TK2MSFTNGP12.phx.gbl...
> If I have a string that is in a constant format of, say 0154321-001, how can
> I parse this into two fragments:
> 
> int contractid = 0154321;
> int contractseq = 001;
> 
> 


Relevant Pages

  • Re: Only allowing alphanumeric characters and _ and -
    ... I think that Regex is a good thing. ... In addition, every time you write an explicit algorithm, you risk writing   ... right syntax for representing some goal. ... time and really learn regular expressions and then it'd be useful. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Thoth filtering question
    ... You can google for a tutorial on regex (regular expressions) for the ... full explanation of the syntax. ...
    (comp.sys.mac.comm)
  • Re: Search for multiple things in a string
    ... >> I also feel that Regular Expressions, being an object in asp.net (not ... So using Regex is not really like using another language (as C# is different ... I agree with you that readability is important. ... And I was not saying experiment with it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: for a laught (???)
    ... Regex doesn't work too well with a null byte delimiter :-) ... the API for a particular form of regular expressions ... Regex doesn't work with null terminated strings. ... qualifier or the qualifier "commonly" might have suggested. ...
    (comp.lang.cobol)
  • Re: how to avoid leading white spaces
    ... motivates my use of regexes. ... You are right that for a simple .startwithusing a regex "just ... That's because you know regex syntax. ... I didn't see anything about de-emphasizing them in Perl. ...
    (comp.lang.python)