Created
May 21, 2012 15:17
-
-
Save kdabir/2762870 to your computer and use it in GitHub Desktop.
finding regex matches in text in groovy
This file contains hidden or 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
def m = "1234 abc" =~ /^(\d+)/ | |
def n = "abc 1234" =~ /^(\d+)/ | |
println (m.find()?m.group():"not matched") | |
println (n.find()?n.group():"not matched") | |
This file contains hidden or 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
// extract number with which line begins | |
def m = "1234 abc" =~ /^(\d+)/ | |
println m[0][1] |
This file contains hidden or 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
// extract all numbers from a line | |
def m = "1234 4567 abc" =~ /(\d+)/ | |
m.each{ println it[0] } | |
// prints 1234 4567 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment