Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I'm converting a project from java to C#. I have no idea what the equivalence of following regular expression would be in C#.

The regular expression : Customer rating (?<rating>\d.\d)/5.0
The java string : "Customer rating (?<rating>\\d.\\d)/5.0"


This is the java code:
Java
private static final Pattern ratingPattern = Pattern.compile("Customer rating (?<rating>\\d.\\d)/5.0");
...
m = Retriever.ratingPattern.matcher(X);
if (m.matches()) {
...
}

and it works for ( X=Customer rating 1.0/5.0 ). But this is the C# code:

C#
static Regex rx = new Regex(@"Customer rating (?<rating>\\d.\\d)/5.0");
...
MatchCollection matches = rx.Matches(X);
if (matches.Count > 0)
{
...
}

and it doesn't work for ( X=Customer rating 1.0/5.0 ).I mean (Matches.count) is always 0 for ( X=Customer rating 1.0/5.0 )

Please help me if you have any ideas.

Thank you
Saman
Posted
Updated 7-Nov-14 4:57am
v5
Comments
PIEBALDconsult 7-Nov-14 9:34am    
You may want to escape the dots.
PIEBALDconsult 7-Nov-14 10:45am    
I get
System.ArgumentException: parsing "Customer rating (?\\d.\\d)/5.0" - Unrecognized grouping construct.

Edit: That was because the code you posted left off the name of the group (rating) -- I fixed it.

First, remove the @, you don't need it when you have the backslashes escaped already.
Saman With You 7-Nov-14 10:48am    
Great! I removed @ and it was solved! Now it works! Thank you so much.
PIEBALDconsult 7-Nov-14 10:51am    
OK, but you should still escape the dots in the pattern; they mean "any character" and you probably want to match actual dots.

Regular Expressions[^] do not change for a different language.

The current regular expression in it's form should work in C# as well.
 
Share this answer
 
Comments
PIEBALDconsult 7-Nov-14 9:33am    
My understanding is that Regular Expressions in .net has a few more features than some other implementations.
I don't know about Java.
Saman With You 7-Nov-14 9:38am    
Yes. It doesn't find any matches in C# when I copy the same string as "Matches" method parameter.
PIEBALDconsult 7-Nov-14 9:39am    
Provide some examples. Have you tried Expresso?
Saman With You 7-Nov-14 9:51am    
No, How can I use it for this problem?
Saman With You 7-Nov-14 9:47am    
This is the java code:

private static final Pattern ratingPattern = Pattern.compile("Customer rating (?<rating>\\d.\\d)/5.0");
...
m = Retriever.ratingPattern.matcher(X);
if (m.matches()) {
...
}

and it works. But this is the C# code:

static Regex rx = new Regex(@"Customer rating (?<rating>\\d.\\d)/5.0");
...
MatchCollection matches = rx.Matches(X);
if (matches.Count > 0)
{
...
}

and it doesn't work. Matches.count is always 0.
Try this:
C#
string pattern = @"Customer rating (\d.\d)/5.0"

Regular Expression Language - Quick Reference[^]
Regular Expression Syntax[^]
http://www.regular-expressions.info/reference.html[^] - very good site with tons of examples!
 
Share this answer
 

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