将特定正则表达式模式从java转换为c# [英] Converting a specific regular expression pattern from java to c#

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

问题描述

你好,



c#中的等价正则表达式是什么?



Hello,

What would be the equivalence regular expression in c# for

private static final Pattern verifiedSourcePattern = Pattern.compile("(\\QCustomer review from the Amazon Vine™ Program\\E)|(\\QAmazon Verified Purchase\\E)");





哪个是Java 。当我在C#中写相同内容时出错:





Which is in Java. I get an error when I write the same in C#:

static Regex verifiedSourcePattern = new Regex("(\\QCustomer review from the Amazon Vine™\\E)|(\\QAmazon Verified Purchase\\E)");









谢谢

Saman





Thank you
Saman

推荐答案

我一直无法确定 \Q 的含义是什么,但在Perl中,它是:



I have been unable to determine what the meaning of \Q is in Java, but in Perl, it's:

\Q          quote (disable) pattern metacharacters until \E
\E          end either case modification or quoted section,





这不会出现在.net实现中,在你的例子中似乎不用了。



删除 \ Q \ E 然后再试一次。





编辑:这在Java中意味着同样的事情......





This doesn't appear in the .net implementation and seems needless in your example.

Remove the \Q and \E and try again.


It means the same thing in Java...

There are two ways to force a metacharacter to be treated as an ordinary character:
•precede the metacharacter with a backslash, or
•enclose it within \Q (which starts the quote) and \E (which ends it).



https://docs.oracle.com/javase/tutorial/essential/regex/literals.html [ ^ ]





此外,作为参考,这里有.net正则表达式的一些文档

http://msdn.microsoft.com/en-us/library/az24scfc(v = vs.110).aspx [ ^ ]


问题是:您的观察结果不正确。可能你只是错误地复制了文本,甚至可能复制了两次。在一个地方你有(\\QCustomer ...),在另一个地方 - (\ QCustomer ...)。

两种情况都不正确。



第一种情况:在C#中,字符串值=(\\QCustomer ... \\ E);

汇编。这意味着值是(\ QCustomer ... \ E)。但这两个转义序列在.NET正则表达式中不存在。



第二种情况:C#尝试用某些东西替换\ Q,但这是一个不存在的转义语法。你可以再次使用'@'。但同样,正则表达式也没有多大意义。







所以,这个代码不正确:

The problem is: your observation is incorrect. Probably, you just copied the text incorrectly, maybe even two times. In one place you have "(\\QCustomer ...)", in another place — "(\QCustomer ...)".
Both cases are incorrect.

First case: in C#, having string value = "(\\QCustomer ...\\E)";
It compiles. It means that value is "(\QCustomer ...\E)". But these two escape sequences do not exist in .NET regular expressions.

The second case: C# tries to replace "\Q" with something, but this is a non-existing escape syntax. You could use '@' again. But again, the regular expression would make not much sense.



So, this code would be incorrect:
using Regex = System.Text.RegularExpressions.Regex;

//...

// won't compile: 'Unrecognized escape sequence'
//Regex regexOne = new Regex("(\QAmazon Verified Purchase\E)");

// will compile, but this line will throw exception
Regex regexTwo = new Regex("(\\QAmazon Verified Purchase\\E)");
// 'Unrecognized escape sequence \Q'

[结束编辑]



请修理并再试一次。如需进一步的帮助,我们需要了解正式和正确描述的目标。



-SA


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

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