문제

I am making an infix evaluator in which the legal tokens include: +, -, *, /, (, ), non-negative integers, and any String that begins with one or more letters and ends with one or more digits.

I am trying to find the most efficient way to determine if a given String begins with one or more letters and ends with one or more digits. The catch is that the alphabetical characaters must come before the numerical values (e.g. X1, XX1, X11). However, if the String contains something similar to 1X, X1X, X#1, then the input is invalid.I know this encloses many possibilities, and I hope there is a way to simplify it.

Thus far I have researched methods such as the String's Any, StartsWith, and EndsWith functions. I just feel like there are too many possibilities to simplify this into a short lambda expression or one-liner. In fact, since we aren't necessarily guaranteed any kind of input, it seems that all N characters would have to be check in order to ensure that the these conditions be met.

Below is the code that I have thus far. This code includes breaking the input String up based on the RegularExpression @"([()+*/-])"

    public static string[] parseString(String infixExp)
    {
        /* In a legal expression, the only possible tokens are (, ), 
         * +, -, *, /, non-negative integers, and strings that begin 
         * with one or more letters and end with one or more digits.
         */

        // Ignore all whitespace within the expression.
        infixExp = Regex.Replace(infixExp, @"\s+", String.Empty);

        // Seperate the expression based on the tokens (, ), +, -, 
        // *, /, and ignore any of the empty Strings that are added
        // due to duplicates.
        string[] substrings = Regex.Split(infixExp, @"([()+*/-])").Where(s => s != String.Empty).ToArray();

        // Return the resulting substrings array such that it
        // can be processed by the Evaluate function.
        return substrings;
    }

If you have any suggestive approaches and/or any references such that I could solve this issue, please feel free!

도움이 되었습니까?

해결책 2

To determine if a given string begins with one or more letters and ends with one or more digits, you can use the the static IsMatch function from the Regex class.

Regex.IsMatch: 
Indicates whether the regular expression finds a match in the input string.

Then you can supply the regular expression ^[A-Z]+[0-9]+$ as the second parameter to the method . Likewise, if you wish to ignore case then you can supply the RegexOptions RegexOptions.IgnoreCase

This will ensure that a string input begins with at least one character and is followed by at least one digit (e.g. XX1, X3, XZ9).

More info on the Regular Expression Language can be found via this link. I hope this helps anyone in the future!

다른 팁

You can try

char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
char[] numbers = "1234567890".ToCharArray();
foreach(string s in infixExp.Split(' '))
{
  if(letters.Contains(s[0]))
  {
    //do stuff
  }
  if(numbers.Contains(s[s.Length-1]))
  {
   //do more stuff
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top