* break expression if match() fails

This commit is contained in:
Reinhard Pointner 2011-11-08 10:41:40 +00:00
parent c61d9c8d0d
commit ae06df9312
2 changed files with 11 additions and 11 deletions

View File

@ -24,6 +24,12 @@ Number.metaClass.compareTo = { String other -> delegate.toString().compareTo(oth
String.metaClass.pad = Number.metaClass.pad = { length = 2, padding = "0" -> delegate.toString().padLeft(length, padding) } String.metaClass.pad = Number.metaClass.pad = { length = 2, padding = "0" -> delegate.toString().padLeft(length, padding) }
/**
* Return a substring matching the given pattern or break.
*/
String.metaClass.match = { def matcher = delegate =~ it; if (matcher.find()) return matcher[0] else throw new Exception("Match failed") }
/** /**
* Use empty string as default replacement. * Use empty string as default replacement.
*/ */
@ -54,12 +60,6 @@ String.metaClass.upperInitial = { replaceAll(/\b[a-z]/, { it.toUpperCase() }) }
String.metaClass.lowerTrail = { replaceAll(/\b(\p{Alpha})(\p{Alpha}+)\b/, { match, initial, trail -> initial + trail.toLowerCase() }) } String.metaClass.lowerTrail = { replaceAll(/\b(\p{Alpha})(\p{Alpha}+)\b/, { match, initial, trail -> initial + trail.toLowerCase() }) }
/**
* Return a substring matching the given pattern or nothing at all.
*/
String.metaClass.match = { def matcher = delegate =~ it; matcher.find() ? matcher[0] : "" }
/** /**
* Return substring before the given pattern. * Return substring before the given pattern.
*/ */

View File

@ -274,7 +274,7 @@
<code><span class="method">pad</span>(<span class="numeral">length</span>, <span class="string">padding</span> = <span class="string">"0"</span>)</code>Pad strings or numbers with given characters ('0' by default). <code><span class="method">pad</span>(<span class="numeral">length</span>, <span class="string">padding</span> = <span class="string">"0"</span>)</code>Pad strings or numbers with given characters ('0' by default).
</p> </p>
<p> <p>
<code><span class="method">replaceAll</span>(<span class="regex">pattern</span>, <span class="string">replacement</span> = <span class="string">""</span>)</code>Replace or remove all occurrences of the pattern. <code><span class="method">match</span>(<span class="regex">pattern</span>)</code>Get a substring matching the given pattern or break.
</p> </p>
<p> <p>
<code><span class="method">space</span>(<span class="string">replacement</span>)</code>Replace all spaces (e.g. "Doctor Who" -> "Doctor_Who"). <code><span class="method">space</span>(<span class="string">replacement</span>)</code>Replace all spaces (e.g. "Doctor Who" -> "Doctor_Who").
@ -286,15 +286,15 @@
<code><span class="method">lowerTrail</span>()</code>Lower-case all letters that are not initials (e.g. "Gundam SEED" -> "Gundam Seed"). <code><span class="method">lowerTrail</span>()</code>Lower-case all letters that are not initials (e.g. "Gundam SEED" -> "Gundam Seed").
</p> </p>
<p> <p>
<code><span class="method">match</span>(<span class="regex">pattern</span>)</code>Get a substring matching the given pattern or nothing at all.
</p>
<p>
<code><span class="method">before</span>(<span class="regex">pattern</span>)</code>Get the substring before the given pattern or the original value. <code><span class="method">before</span>(<span class="regex">pattern</span>)</code>Get the substring before the given pattern or the original value.
</p> </p>
<p> <p>
<code><span class="method">after</span>(<span class="regex">pattern</span>)</code>Get the substring after the given pattern or the original value. <code><span class="method">after</span>(<span class="regex">pattern</span>)</code>Get the substring after the given pattern or the original value.
</p> </p>
<p> <p>
<code><span class="method">replaceAll</span>(<span class="regex">pattern</span>, <span class="string">replacement</span> = <span class="string">""</span>)</code>Replace or remove all occurrences of the pattern.
</p>
<p>
<code><span class="method">replaceTrailingBrackets</span>(<span class="string">replacement</span> = <span class="string">""</span>)</code>Replace trailing parenthesis including any leading whitespace (e.g. "The IT Crowd (UK)" -> "The IT Crowd"). <code><span class="method">replaceTrailingBrackets</span>(<span class="string">replacement</span> = <span class="string">""</span>)</code>Replace trailing parenthesis including any leading whitespace (e.g. "The IT Crowd (UK)" -> "The IT Crowd").
</p> </p>
<p> <p>