Determining if a String begins with an Alphabetical Char and Contains
Numerical Digits
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!
No comments:
Post a Comment