Last active
August 29, 2015 14:13
-
-
Save kknd22/cdf0b24ecc0ffa7fd965 to your computer and use it in GitHub Desktop.
groovy regular expression replacement foo-bar example
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
// matcher | |
def m = "foo-bar" =~ /(.+)(-)(.+)/ | |
println m[0] | |
println m[0][1] | |
println m[0][2] | |
println m[0][3] | |
assert m[0] == ["foo-bar", "foo", "-", "bar"] | |
def i = "foo-bar" | |
def o = i.replaceAll(/(.+)-(.+)/){zero, one, two -> one + two.capitalize()} | |
assert o == "fooBar" | |
println o | |
// -- http://naleid.com/blog/2008/05/19/dont-fear-the-regexp | |
def dashedToCamelCase(orig) { | |
// regular expression is a dash, followed by parenthesis that form a group where we hold the word's first character | |
orig.replaceAll(/-(\w)/) { | |
fullMatch, firstCharacter -> println fullMatch | |
firstCharacter.toUpperCase() } | |
} | |
assert "firstName" == dashedToCamelCase("first-name") | |
assert "oneTwoThreeFourFiveSixSevenEight" == dashedToCamelCase("one-two-three-four-five-six-seven-eight") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment