0

I need to sort the string based on the following conditions...

here is the input "100AP12345678"

need output like 100 in one string AP in another and 1234567890 in another..

I did like below

protected void Button1_Click(object sender, EventArgs e)
    {
        var numAlpha = new Regex("(?<Numeric>[0-9]+)(?<Alpha>[a-zA-Z]*)");



        var input=TextBox1.Text;

        var match = numAlpha.Match(TextBox1.Text);

        var Amount = match.Groups["Numeric"].Value;

        var Operator = match.Groups["Alpha"].Value;

        var index = Operator.Substring(Operator.Length - 1);

        var MobileNum = input.Substring(input.IndexOf(index) + 1);

        var kk = numAlpha.Match(MobileNum).Groups["Alpha"].Value;

        if ((Operator.Length > 2) & ((numAlpha.Match(MobileNum).Groups["Alpha"].Value).Length != 0) || (MobileNum.Length > 10))
        {
            Label1.Text = Amount;
            Label3.Text = "invalid MobileNum";
            Label2.Text = "invalid operator";
        }

        else if (((numAlpha.Match(MobileNum).Groups["Alpha"].Value).Length != 0) & (MobileNum.Length > 10))
        {
            Label1.Text = Amount;
            Label2.Text = Operator;
            Label3.Text = "invalid MobileNum";
        }
        else if (Operator.Length > 2)
        {
            Label1.Text = Amount;
            Label2.Text = "invalid operator";
            Label3.Text = MobileNum;
        }
        else
        {
            Label1.Text = Amount;
            Label2.Text = Operator;
            Label3.Text = MobileNum;
        }
    }


first charcaters i.e numbers are rupees and next two are operator and the remaining are mobile number

conditions:

1.amount will have no limitation
2.operator should be two characters
3.mobileNum should not exceed 10

the above code works good

but two limitations 1. when I am passing input starting with alphabets it doesnt work

like ab1001234567890 as I have given regex function as numbers and alphabets and second limitation is when i Pass operator like aba its not showing invalid operator as I used Substring..
Everything works good if I pass input like
100VD9441239494

here two limitations in the code

1.If I pass VD1009441239494 doesnt work n achevie my requirements

  1. if i pass reapareted characters in operartor like
    100VDV9441239494 not getting perfect output as I used Substring
12
  • which fields are optional? Commented Feb 7, 2015 at 9:48
  • No fileld is Optional filed..... Commented Feb 7, 2015 at 9:50
  • 1.amount will have no limitation 2.operator should be two characters 3.mobileNum should not exceed 10 like 10000VD9441239494 Commented Feb 7, 2015 at 9:50
  • but this lk109032030002 misses the first part. Commented Feb 7, 2015 at 9:51
  • it is working good when i pass the input like 10000VD9441239494 and if i pass like VD1009441239494 it doesnt Commented Feb 7, 2015 at 9:51

3 Answers 3

3

AS u have only one exception as u said u can make use of try n catch blocks and run n test d code works good

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            var numAlpha = new Regex(@"^(?<Numeric>[0-9]+)(?<Alpha>[a-zA-Z]+)(?<mnum>\d+)$");

            var input = TextBox1.Text;

            var match = numAlpha.Match(TextBox1.Text);

            var Amount = match.Groups["Numeric"].Value;

            var Operator = match.Groups["Alpha"].Value;

                var index = Operator.Substring(Operator.Length - 1);

                var MobileNum = input.Substring(input.IndexOf(index) + 1);


            var kk = numAlpha.Match(MobileNum).Groups["Alpha"].Value;

            if ((Operator.Length > 2) & ((numAlpha.Match(MobileNum).Groups["Alpha"].Value).Length != 0) || (MobileNum.Length > 10))
            {
                Label1.Text = Amount;
                Label3.Text = "invalid MobileNum";
                Label2.Text = "invalid operator";
            }

            else if (((numAlpha.Match(MobileNum).Groups["Alpha"].Value).Length != 0) & (MobileNum.Length > 10))
            {
                Label1.Text = Amount;
                Label2.Text = Operator;
                Label3.Text = "invalid MobileNum";
            }
            else if (Operator.Length > 2)
            {
                Label1.Text = Amount;
                Label2.Text = "invalid operator";
                Label3.Text = MobileNum;
            }
            else
            {
                Label1.Text = Amount;
                Label2.Text = Operator;
                Label3.Text = MobileNum;
            }
        }
        catch(Exception ex)
        {
            Label4.Text = "invalid input";
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

One more regex which will match your criteria. The first group will give you the rupees, second group will give your the operator and third group will give you the mobile number.

\b(\d+)([a-zA-Z]{2})(\d{10})\b

\b - Start of a word boundary.
(\d+) - Will match digits (rupees).
([a-zA-Z]{2}) - Will match the 2 character operator.
(\d{10}) - Will match the 10 digit telephone number.
\b - End of word boundary.

You can have a regex demo here.

Comments

0

You need to use anchors.

var numAlpha = new Regex(@"^(?<Numeric>[0-9]+)(?<Alpha>[a-zA-Z]+)(?<mnum>\d+)$");

OR

var numAlpha = new Regex(@"^(?<Numeric>[0-9]+)(?<Alpha>[a-zA-Z]{2})(?<mnum>\d{10})$");
  • ^ Start
  • \d+ One or more digits.
  • [a-zA-Z]+ One or more alphabets.
  • $ End.

5 Comments

If I replaced that then I cant able to retrive the amount value if i given i/p like lk109032030002..
means I need to display as wrong i/p after submitt Button
then got error like StartIndex cannot be less than zero. at var index = Operator.Substring(Operator.Length - 1);
problem is not with the regex.
@ManoharJonnalagadda you the one said "resolved" then why you unaccept my answer? I'm the one who provides a correct regex at very first.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.