Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h2>RegExp</h2><h3>matcher.matches() returns false</h3><p>Why does this code fail?</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def matcher = "/home/me/script/test.groovy" =~ /\.groovy/ assert matcher.matches() </pre></td></tr></table><p>Because you think you do something like <em>"Oh dear it contains the word!"</em>, but you're confusing <em>matches</em> with <em>find</em></p><p>From Javadoc: <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#matches()">http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#matches()</a></p><p><span style="color: blue;">public boolean matches()</span><br /> <span style="color: blue;">Attempts to match the</span> <span style="color: red;"><strong>entire</strong></span> <span style="color: blue;">region against the pattern.</span><br /> <span style="color: blue;">...</span></p><p>So "<em>/\.groovy/</em>" is just a subsequence.<br /> You must use</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def matcher = "/home/me/script/test.groovy" =~ /.*\.groovy/ </pre></td></tr></table><h3>What is the difference between =~ and ==~ ?</h3><ul><li>~ is the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html">Pattern</a> symbol.</li><li>=~ means <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#find()">matcher.find()</a></li><li>==~ means <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#matches()">matcher.matches()</a></li></ul><h4>Pattern, Matcher ?</h4><p>A pattern is not very useful alone. He's just waiting input to process through a <em>matcher</em>.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def pattern = ~/groovy/ def matcher = pattern.matcher('my groovy buddy') </pre></td></tr></table><p>Matcher can say a lot of thing to you:</p><ul><li>if the <em>entire</em> input sequence matches the pattern, with <code><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#matches()">matcher.matches()</a></code> ;</li><li>if just a <em>subsequence</em> of the input sequence matches the pattern, with <code><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#find()">matcher.find()</a></code>.</li></ul><p>A matcher with /groovy/ pattern <strong>finds</strong> a matching subsequence in the 'my groovy buddy' sequence.<br /> On the contrary the whole sequence doesn't <strong>match</strong> the pattern.</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def m = c.matcher('my groovy buddy') assert m.find() assert m.matches() == false </pre></td></tr></table><p>Application: to filter a list of names.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=groovy" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9Z3Jvb3Z5fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>def list = ['groovy', '/a/b/c.groovy', 'myscript.groovy', 'groovy.rc', 'potatoes', 'groovy.'] // find all items whom a subsequence matches /groovy/ println list.findAll{ it =~ /groovy/ } // => [ "groovy", "/a/b/c.groovy", "myscript.groovy", "groovy.rc", "groovy." ] // find all items who match exactly /groovy/ println list.findAll{ it ==~ /groovy/ } // => [ "groovy" ] // find all items who match fully /groovy\..*/ ('groovy' with a dot and zero or more char trailing) println list.findAll{ it ==~ /groovy\..*/ } // => ["groovy.rc", "groovy."] </pre></td></tr></table><p>A little tilde headache ? Remember like this</p><table class="confluenceTable"><tbody><tr><td class="confluenceTd"><p>~</p></td><td class="confluenceTd"><p>the pattern</p></td></tr><tr><td class="confluenceTd"><p>=~</p></td><td class="confluenceTd"><p>roughly as the pattern (easy to write)</p></td></tr><tr><td class="confluenceTd"><p>==~</p></td><td class="confluenceTd"><p>more than roughly, <strong>exactly</strong> as the pattern (think hard...)</p></td></tr></tbody></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced