Regular expression, numbers?
I made a code that translates strings to match each word from array 0ne to array two and display the correct results. But how to let the compiler take the number on the line and print it as it is, ummmm see the code I wrote
class Program
{
public static string[] E = { "i", "go", "school", "to", "at" };
public static string[] A = { "Je", "vais", "ecole", "a", "a" };
public static string Translate(string s)
{
string str = "";
Regex Expression = new Regex(@"[a-zA-Z]+");
MatchCollection M = Expression.Matches(s);
foreach (Match x in M)
str = str + " " + TranslateWord(x.ToString());
return str;
}
public static string TranslateWord(string s)
{
for (int i = 0; i < E.Length; i++)
if (s.ToLower() == E[i].ToLower())
return A[i];
return "Undefined";
}
here I want to enter the whole string and the code has to translate it with a number, now I know how to make a word (by merging and translating them) but what about numbers)
static void Main(string[] args)
{
string str = "I go to school at 8";
Console.WriteLine(Translate(str));
}
how to continue ?!
0
Sara
source
to share
4 answers
Here's a hint:
public static void process (String s) {
String [] tokens = s.split("\\s+");
for (String token : tokens) {
if (token.matches("[A-Za-z]+")) {
System.out.println(" word: '" + token + "'");
} else if (token.matches("[0-9]+")) {
System.out.println("number: '" + token + "'");
} else {
System.out.println(" mixed: '" + token + "'");
}
}
}
Wnen, called for example ...
process("My 23 dogs have 496 fleas.");
... it produces the following:
word: 'My'
number: '23'
word: 'dogs'
word: 'have'
number: '496'
mixed: 'fleas.'
0
source to share