tags:

views:

436

answers:

4

Hi guys, I made a code that translate strings to match each word from the array 0ne to the array two and its showing the right results. But how to let the compiler take the number in the string and print it out 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 the whole string and the code should translate it with the number, now i know how to do the word (by spliting them and translate) but what about the numbers)

        static void Main(string[] args)
        {
            string str = "I go to school at 8";
            Console.WriteLine(Translate(str));
        }

how to continue ?!

+4  A: 

Change your regex to [a-zA-Z0-9]+

By the way, why don't you use String.Split method instead of Regex?

Mehrdad Afshari
well i want to practice on the regex more and more its simple by split
You can't really practice regular expressions by making up situations. Regular expressions are only really useful when you have unknown input.
Gareth
Gareth, you're right in general, but step by step learning is good. At very least it makes you good at using the Regex library methods.
Mehrdad Afshari
A: 

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 invoked with e.g. ...

process("My 23 dogs have 496 fleas.");

...it produces the following:

  word: 'My'
number: '23'
  word: 'dogs'
  word: 'have'
number: '496'
 mixed: 'fleas.'
joel.neely
What language is that?
configurator
Java (Java 5, to be precise).
joel.neely
+1  A: 

This regular expression will work better when you actually start typing the accents on your French words, and you want to split a French string:

\w+

In .NET, \w includes all letters and numbers from all scripts, not just the English a-z and 0-9.

Jan Goyvaerts
A: 

If your regex engine supports it, I use [:alnum:] (i.e. POSIX classes), makes for more portable regexp. As usual, beware of locale issues.

Keltia