Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add features missing from the Java regex implementation?
    primarykey
    data
    text
    <p>I'm new to Java. As a .Net developer, I'm very much used to the <code>Regex</code> class in .Net. The Java implementation of <code>Regex</code> (Regular Expressions) is not bad but it's missing some key features.</p> <p>I wanted to create my own helper class for Java but I thought maybe there is already one available. So is there any free and easy-to-use product available for Regex in Java or should I create one myself?</p> <p>If I would write my own class, where do you think I should share it for the others to use it?</p> <hr> <p><strong>[Edit]</strong></p> <p>There were complaints that I wasn't addressing the problem with the current <code>Regex</code> class. I'll try to clarify my question.</p> <p>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.</p> <hr> <p>Here's a little code compared in Java and C#. The first is C# and the second is Java:</p> <p><strong>In C#:</strong></p> <pre class="lang-C# prettyprint-override"><code>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); } </code></pre> <p><strong>In Java:</strong></p> <pre class="lang-java prettyprint-override"><code>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())); } </code></pre> <p>I tried to be fair to both languages in the sample code above.</p> <p>The first thing you notice here is the <code>.Value</code> member of the <code>Match</code> class (compared to using <code>.start()</code> and <code>.end()</code> in Java).</p> <p>Why should I create two objects when I can call a static function like <code>Regex.Matches</code> or <code>Regex.Match</code>, etc.?</p> <p>In more advanced usages, the difference shows itself much more. Look at the method <code>Groups</code>, dictionary length, <code>Capture</code>, <code>Index</code>, <code>Length</code>, <code>Success</code>, etc. These are all very necessary features that in my opinion should be available for Java too.</p> <p>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 <code>Regex</code> in Perl but at least we can use the .Net approach to <code>Regex</code> which I think is very cleverly designed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload