Last active
August 29, 2015 14:13
-
-
Save jxc876/9728dae3065a102cca0e to your computer and use it in GitHub Desktop.
Groovy Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// String, need to escape backslashes | |
def bs = '\\\\' | |
// Slashy Strings: /abc/ | |
// String literal not needing additional backslashes to escape special characters. | |
def path = /C:\dev\source/ | |
// G-Strings (double-quotes) | |
// Can use templating: ${expression} syntax | |
// Any valid Groovy expression can be enclosed, incl method calls | |
println "The $foxtype ${foxcolor.join()} fox" | |
// Multi-Line Strings | |
// use """ | |
def text = """\ | |
hello there ${name} | |
how are you today? | |
""" | |
// Pattern: ~/abc/ | |
// see java.util.regex.Pattern | |
def pattern = ~/.*.java/ | |
// Create Matcher: =~ | |
// Returns a Matcher (java.util.regex.Matcher) | |
def matcher = (input =~ regex) | |
int MATCH1 = 0, GROUP1 = 0 | |
String result = matcher[MATCH1][GROUP1] | |
// test: =~ | |
// a Matcher coerces to a boolean by calling its find() method | |
// requires only a subsequence of the string to match | |
// "true" if it has at least one match | |
if (matcher) | |
if (input =~ regex) | |
// test: ==~ | |
// requires an exact match of the whole string, matches() | |
// returns a Boolean, not a Matcher. | |
if ("foo" ==~ /aRegex/ ) | |
assert "foo" ==~ /aRegex/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment