Как добавить функции, отсутствующие в реализации регулярного выражения Java?

Я новичок в Java. Как разработчик .Net, я очень привык к классу Regex в .Net. Реализация Java Regex (регулярные выражения) неплохая, но в ней отсутствуют некоторые ключевые функции.

Я хотел создать свой собственный вспомогательный класс для Java, но подумал, что, возможно, он уже доступен. Итак, есть ли какой-нибудь бесплатный и простой в использовании продукт для Regex на Java, или я должен создать его сам?

Если бы я написал свой собственный класс,


[Edit]

There were complaints that I wasn't addressing the problem with the current Regex class. I'll try to clarify my question.

In .Net the usage of a regular expression is easier than in Java. Since both languages are object oriented and very similar in many aspects, I expect to have a similar experience with using regex in both languages. Unfortunately that's not the case.


Here's a little code compared in Java and C#. The first is C# and the second is Java:

In C#:

string source = "The colour of my bag matches the color of my shirt!";
string pattern = "colou?r";

foreach(Match match in Regex.Matches(source, pattern))
{
    Console.WriteLine(match.Value);
}

In Java:

String source = "The colour of my bag matches the color of my shirt!";
String pattern = "colou?r";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(source);

while(m.find())
{
    System.out.println(source.substring(m.start(), m.end()));
}

I tried to be fair to both languages in the sample code above.

The first thing you notice here is the .Value member of the Match class (compared to using .start() and .end() in Java).

Why should I create two objects when I can call a static function like Regex.Matches or Regex.Match, etc.?

In more advanced usages, the difference shows itself much more. Look at the method Groups, dictionary length, Capture, Index, Length, Success, etc. These are all very necessary features that in my opinion should be available for Java too.

Of course all of these features can be manually added by a custom proxy (helper) class. This is main reason why I asked this question. We don't have the breeze of Regex in Perl but at least we can use the .Net approach to Regex which I think is very cleverly designed.

25
задан Maarten Bodewes 10 January 2017 в 15:44
поделиться