Code to split x³

Kalyan A 440 Reputation points
2024-10-11T17:35:16.54+00:00

I got help from the for to split exponents , but the below code won't split x³

I want output as x and 3

using System;
using System.Text;
using System.Text.RegularExpressions;
string input = "2³";
Regex regex = new Regex(@"(\d+)([\xB2\xB3\xB9\u2070\u2074-\u2079]+)");
Match match = regex.Match(input);
if (match.Success)
{
    int baseNum = int.Parse(match.Groups[1].Value);
    System.Console.WriteLine(baseNum);
   
   int exponent = int.Parse( match.Groups[2].Value.Normalize( NormalizationForm.FormKC ) );
    System.Console.WriteLine(exponent);
    
    
}
else
{
    Console.WriteLine("No match found.");
}
 
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-10-15T01:45:10.6166667+00:00

    Hi @Kalyan A , Welcome to Microsoft Q&A,

    Since QA cannot mark his own questions, I will repost your answer to close this case. So the solution to the issue is as follow:

    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    class Program
    {
        static void Main()
        {
            string input = "2y²x³";
            int len1 =   input.Length;;
          //  Console.WriteLine(len1);
            string invalidPattern = @"[a-zA-Z]\d";
            bool containsInvalid = Regex.IsMatch(input, invalidPattern);
            string patternx = @"^[a-zA-Z0-9]+$";
            Regex regex1 = new Regex(patternx);
           containsInvalid = Regex.IsMatch(input, patternx);
             if (ContainsLettersAndDigitsOnly(input))
            { containsInvalid =false; }
             else
            { containsInvalid = true; }
            if (containsInvalid)
            {
                Console.WriteLine("Invalid monomials found (letters followed by digits):");
            }
            else
            {
                // Regex pattern to split by superscripts, letters, and digits
                string pattern = @"([\xB2\xB3\xB9\u2070\u2074-\u2079]+|[a-zA-Z]|\d)";
                // Use Regex.Split to split the input string based on the given pattern
                string[] result = Regex.Split(input, pattern);
                // Output the results, filtering out empty entries
                foreach (string s in result)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        Console.WriteLine(s.Normalize(NormalizationForm.FormKC));
                    }
                }
            }
            static bool ContainsLettersAndDigitsOnly(string input)
            {
                // Regular expression to check if the string contains both letters and digits, and no other characters
                return Regex.IsMatch(input, @"^[A-Za-z\d\u00B2\u00B3\u2070-\u207F]+$");
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kalyan A 440 Reputation points
    2024-10-12T09:43:49.7+00:00
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    class Program
    {
        static void Main()
        {
            string input = "2y²x³";
            int len1 =   input.Length;;
          //  Console.WriteLine(len1);
            string invalidPattern = @"[a-zA-Z]\d";
            bool containsInvalid = Regex.IsMatch(input, invalidPattern);
            string patternx = @"^[a-zA-Z0-9]+$";
            Regex regex1 = new Regex(patternx);
           containsInvalid = Regex.IsMatch(input, patternx);
             if (ContainsLettersAndDigitsOnly(input))
            { containsInvalid =false; }
             else
            { containsInvalid = true; }
            if (containsInvalid)
            {
                Console.WriteLine("Invalid monomials found (letters followed by digits):");
            }
            else
            {
                // Regex pattern to split by superscripts, letters, and digits
                string pattern = @"([\xB2\xB3\xB9\u2070\u2074-\u2079]+|[a-zA-Z]|\d)";
                // Use Regex.Split to split the input string based on the given pattern
                string[] result = Regex.Split(input, pattern);
                // Output the results, filtering out empty entries
                foreach (string s in result)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        Console.WriteLine(s.Normalize(NormalizationForm.FormKC));
                    }
                }
            }
            static bool ContainsLettersAndDigitsOnly(string input)
            {
                // Regular expression to check if the string contains both letters and digits, and no other characters
                return Regex.IsMatch(input, @"^[A-Za-z\d\u00B2\u00B3\u2070-\u207F]+$");
            }
        }
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.