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.