-2

how can i make a function that extracts digits and letters from tokens,i am trying to work on a lexical analyzer in c sharp which extracts variables datatypes liberaries from a cpp file i want my function to extract digits and letters i have managed to make functions that extract variables ,datatypes n libraries

for example i want a combination of these two functions

bool IsDigit(char ch)
{
    return (ch >='0' && ch <= '9');
}

bool IsAlpha(char ch)
{
    return (ch >='a' && ch <= 'z'|| ch>'A '&& ch<'Z');

}

any ideas??

4

3 回答 3

1

Are you trying to do this:

private bool IsAlphaNumeric(string letter)
{
     return Regex.IsMatch(letter, @"^[a-zA-Z0-9]");
}
于 2012-11-08T19:40:50.983 回答
0

You can try this:-

 Match match = regex.Match("10ABCD");
 string letter = match.Groups["letter"].Value;
  int number = int.Parse(match.Groups["number"].Value);

or try a regular expression:-

 ((?<number>\d+)(?<letter>[a-zA-Z])|(?<letter>[a-zA-Z])(?<number>\d+))
于 2012-11-08T19:30:38.187 回答
0

Well first off, you should be doing this on IsAlpha (notice the extra brackets)

((ch >='a' && ch <= 'z')||(ch>'A '&& ch<'Z'))
于 2012-11-08T19:31:17.230 回答