Click here to Skip to main content
15,881,769 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
Hello,

What would be the equivalence regular expression in c# for

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


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

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




Thank you
Saman
Posted
Comments
PIEBALDconsult 10-Nov-14 18:40pm    
What error?
Saman With You 10-Nov-14 18:44pm    
Hi, parsing "(\QCustomer review from the Amazon Vine™\E)|(\QAmazon Verified Purchase\E)" - Unrecognized escape sequence \Q.
PIEBALDconsult 10-Nov-14 18:49pm    
And what is the goal? Why are you use \Q and \E in that expression? As far as I can tell, they aren't serving any purpose.
PIEBALDconsult 10-Nov-14 19:10pm    
Also, try Expresso, it might give some insight.
http://www.ultrapico.com/Expresso.htm

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,


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

Remove the \Q and \E and try again.


Edit: 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[^]


Also, as reference, here's some documentation of .net Regular Expressions
http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx[^]
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 11-Nov-14 10:23am    
5ed.
—SA
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.

[EDIT]

So, this code would be incorrect:
C#
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'
[END EDIT]

Please fix it and try again. For further help, we would need to know the goal, formally and correctly described.

—SA
 
Share this answer
 
v3
Comments
PIEBALDconsult 10-Nov-14 19:07pm    
\Q has a meaning in other implementations. I see it in my Perl book, but its meaning is not in the .net implementation and seems pointless in the given example.
Sergey Alexandrovich Kryukov 10-Nov-14 19:12pm    
Really? And could you tell me what's that meaning?

Anyway, I'm glad to get the confirmation that '\Q' is meaningless in .NET (and in fact is equivalent to 'Q', where '\' is simply redundant). So, it keeps my answer valid and my advice applicable.

I must say, I did not check it up on .NET this time. I checked it with my own native code using 3rd-party regex. So far, I did not face any discrepancies with .NET, but yes, Rerl regex could be different.

—SA
PIEBALDconsult 10-Nov-14 20:41pm    
By "verbose" you mean "verbatim".

In one place you have "(\\QCustomer ...)", in another place — "(\QCustomer ...)". --- I don't see that.
Sergey Alexandrovich Kryukov 10-Nov-14 21:40pm    
Well, "verbatim", thank you.
Two places are: in OP's question and comment.
—SA
PIEBALDconsult 10-Nov-14 21:44pm    
Ah, I ignored the comment; it's non-canon. :D

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