将正则表达式从java转换为.Net [英] Converting regular expression from java to .Net

查看:111
本文介绍了将正则表达式从java转换为.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在将项目从java转换为C#。我不知道下面的正则表达式在C#中的等价性是什么。



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"





这是java代码:



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()) {
...
}



,适用于(X =客户评级1.0 / 5.0)。但这是C#代码:




and it works for ( X=Customer rating 1.0/5.0 ). 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)
{
...
}



并且它不起作用(X =客户评级1.0 /5.0)。我的意思是(Matches.count)总是0(X =客户评级1.0 / 5.0)



如果您有任何想法,请帮助我。



谢谢

Saman


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

推荐答案

正式表达式 [ ^ ]不要换另一种语言。



它的表格中的当前正则表达式也应该在C#中工作。
Regular Expressions[^] do not change for a different language.

The current regular expression in it's form should work in C# as well.


试试这个:

Try this:
string pattern = @"Customer rating (\d.\d)/5.0"



常规表达语言 - 快速参考 [ ^ ]

正则表达式语法 [ ^ ]

http://www.regular-expressions.info/reference.html [ ^ ] - 非常好的网站,有大量的例子!


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


这篇关于将正则表达式从java转换为.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆