Tuesday 1 September 2015

Regular Expression to match numbers inside square brackets

A simple example which will demonstrate how to use Regex to match numeric value inside square brackets.

Example:

 Response.Write(extractOrderid("dflgkj [3423]"));

using System.Text;
string extractOrderid(string msg)
{
        string resp = "";
        Match morderid = Regex.Match(msg, @"\[(\d+)\]");
        if (morderid != null)
        {
            resp = morderid.Value;
        }
        return resp;
}