Click here to Skip to main content
15,908,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm learning C# and finding difficulties in converting this JAVA code into C#. If anyone would provide a solution. Would be better for my understanding.

Java
public static List<Operator> oprs = new List<Operator>();
private List<String> GetTokens(String s)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(Pattern.quote(oprs[0].symbol));

        for (int i = 1; i < oprs.Count; i++)
        {
            sb.Append("|");
            sb.Append(Pattern.quote(oprs.get(i).symbol));
        }
        sb.Append("|[a-zA-Z_]+[a-zA-Z0-9_]*\\([^()]*\\)|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*|\\Q(\\E|\\Q)\\E");

        List<String> tokens = new List<String>();
        Pattern p = Pattern.compile(sb.ToString());
        Matcher m = p.matcher(s);

        while (m.find()) tokens.add(m.group());
        return tokens;
    }


What I have tried:

Try 1
C#
public static List<operator> oprs = new List<operator>();
private List<string> GetTokens(String s)
    {
Dictionary<string,> dictionary = new Dictionary<string,>()
            {
                {source, " " }
            };
            var matches = dictionary.SelectMany(a => Regex.Matches(s, a.Value)
            .Cast<match>()
            .Select(b=>
                    new
                    {
                        Index = b.Index,
                        Value = b.Value,
                        Token = a.Key
                    }))
            .OrderBy(a => a.Value).ToList();
            List<string> tokens = new List<string>();
            foreach(var items in matches)
            {
                tokens.Add(items.Value.ToString());
            }
return tokens;
}


Try 2
C#
private List GetTokens(String s)
{
    List tokens = new List();
    Regex rx = new Regex(@"|[a-zA-Z_]+[a-zA-Z0-9_]*\\([^()]*\\)|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*|\\Q(\\E|\\Q)\\E");

    MatchCollection matches = rx.Matches(s);
    if (matches.Count > 0)
    {
        Match match = matches[0];
        GroupCollection groupCollection = match.Groups;
    }

    foreach(var ma in matches)
    {
        tokens.Add(ma.ToString());
    }

    return tokens;
}
Posted
Updated 21-Apr-16 20:39pm
v3
Comments
Garth J Lancaster 21-Apr-16 22:02pm    
Some observations
1) you dont say whats wrong with the c# version/what it does or doesnt do
2) your c# version is very different from the java - if I were doing this exercise, I'd 'transliterate' ie make the c# match the java as close as possible, Im not convinced thats what you've done - once I had it working 'as close as possible', I'd then do a refactor/rework to make it pretty/optimise it
Member 11666440 21-Apr-16 22:13pm    
Honestly, I had given quite a lot of tries.
Member 11666440 21-Apr-16 22:14pm    
check the new try I have given it.
Member 11666440 21-Apr-16 22:14pm    
private List<string> GetTokens(String s)
{
List<string> tokens = new List<string>();
Regex rx = new Regex(@"|[a-zA-Z_]+[a-zA-Z0-9_]*\\([^()]*\\)|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*|\\Q(\\E|\\Q)\\E");
MatchCollection matches = rx.Matches(s);
if (matches.Count > 0)
{
Match match = matches[0];
GroupCollection groupCollection = match.Groups;

}
foreach(var ma in matches)
{
tokens.Add(ma.ToString());
}

return tokens;
}
Garth J Lancaster 21-Apr-16 22:27pm    
this one is a lot more comparable to your java version. does it work ?

I'm guessing you are going to have to tweak the Regex

Ive copied your try #2 back into your original question - better that way (hint - you should have modified the original question)

1 solution

How about you use one of the converters and then study and tweak the result?

Check here: [6 best java converters]

Note that varycode is currently updating java algorithm and is unavailable online.


Microsoft also provides this functionality

[in VS (see MSDN)]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900