Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matches in C# but not in java

Tags:

java

c#

regex

I have the following regex (long, I know):

(?-mix:((?-mix:(?-mix:\{\%).*?(?-mix:\%\})|(?-mix:\{\{).*?(?-mix:\}\}?))
|(?-mix:\{\{|\{\%)))

that I'm using to split a string. It matches correctly in C#, but when I moved the code to Java, it doesn't match. Is there any particular feature of this regex that is C#-only?

The source is produced as:

String source = Pattern.quote("{% assign foo = values %}.{{ foo[0] }}.");

While in C# it's:

string source = @"{% assign foo = values %}.{{ foo[0] }}.";

The C# version is like this:

string[] split = Regex.split(source, regex);

In Java I tried both:

String[] split = source.split(regex);

and also

Pattern p = Pattern.compile(regex);
String[] split = p.split(source);
like image 840
Tudor Avatar asked Dec 02 '11 20:12

Tudor


1 Answers

Here is a sample program with your code: http://ideone.com/hk3uy

There is a major difference here between Java and other languages: Java does not add captured groups as tokens in the result array (example). That means that all delimiters are removed from result, though they would be included in .Net.
The only alternative I know is not to use split, but getting a list of matches and splitting manually.

like image 52
Kobi Avatar answered Sep 28 '22 00:09

Kobi