1

I need a RegEx guru to help me (I'm a noob). I have the following string that I want to parse out the client ID and contract ID from (using C#). I can do it with a bunch of string functions, but I thought a RegEx might be cleaner. Can anyone help me do this?

Here's an example of the string: "FW: Order Contract - 11009972; Customer - 5424 - TOYOTA CO; AE ID - 160SB Completed"

And I need to get the following values: 11009972 put in a ContractID variable 5424 put in a CustomerID variable

Now the issues are that the ContractID and CustomerID can be any length, but they will always be numeric values, preceeded with "Contract - " or "Customer - " and they will always be in the same order. The rest of the string's text may change in content and length, but it can be discarded. So I want to find the digits following "Contract - " or "Customer - " up until the next space or semicolon (or any non-numeric character).

Any help is greatly appreciated!

3 Answers 3

1

Having the important part of the string always the same format means the regex can be pretty simple.

string text ="FW: Order Contract - 11009972; Customer - 5424 - TOYOTA CO; AE ID - 160SB Completed";
Regex myRegex = new Regex(@"Contract - (\d+); Customer - (\d+)");
var match = myRegex.Match(text);
// match.Groups[1].Value is the ContractId
// match.Groups[2].Value is the CustomerID 
2
  • It looks like all the solutions work. Since you posed first, I'm marking this one as the answer. One question though, what are the differences between the following expressions (what do the extra characters do)? "Contract - (\d+); Customer - (\d+)" versus "^.*Contract - (\d+); Customer - (\d+).*$"
    – bigmac
    Commented Oct 26, 2011 at 17:03
  • @bmccleary You're welcome. regular-expressions.info/reference.html explains all the characters in detail, in brief though () is a group (anything matched in these appears in order in the match.Groups[] array), \d+ means one or more digits, ^.* means everything from the start of the string and .*$ means everything till the end of the string. You don't really need ^.* or .*$ in there in this situation though. Commented Oct 26, 2011 at 17:36
1

Try the following,

string text = "FW: Order Contract - 11009972; Customer - 5424 - TOYOTA CO; AE ID - 160SB Completed";
Match match = Regex.Match(input, @"^.*Contract - (\d+); Customer - (\d+).*$", RegexOptions.IgnoreCase);
if (match.Success)
{
    string contract = match.Groups[1].Value;
    string customer = match.Groups[2].Value;
}
0
0

You can use this ^.*Contract\s*-\s*(\d*).*Customer\s*-\s*(\d*).*$

This puts contract id in group 1 and customer id in group 2.

string subjectString = "FW: Order Contract - 11009972; Customer - 5424 - TOYOTA CO; AE ID - 160SB Completed";
ContractId = Regex.Match(subjectString, 
     @"^.*Contract\s*-\s*(\d*).*Customer\s*-\s*(\d*).*$", 
     RegexOptions.Multiline).Groups[1].Value;
CustomerId = Regex.Match(subjectString, 
     @"^.*Contract\s*-\s*(\d*).*Customer\s*-\s*(\d*).*$",  
     RegexOptions.Multiline).Groups[2].Value;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.