Skip to content

Instantly share code, notes, and snippets.

@kdabir
Created May 21, 2012 15:17
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kdabir/2762870 to your computer and use it in GitHub Desktop.
Save kdabir/2762870 to your computer and use it in GitHub Desktop.
finding regex matches in text in groovy
def m = "1234 abc" =~ /^(\d+)/
def n = "abc 1234" =~ /^(\d+)/
println (m.find()?m.group():"not matched")
println (n.find()?n.group():"not matched")
def text = """
Something
@ab_d @ffg
d
@efg
"""
//def line = "@abd @ffr"
s = text=~/(@[\w-]+)/
while (s.find()) println s.group()
// extract number with which line begins
def m = "1234 abc" =~ /^(\d+)/
println m[0][1]
// 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