DEV Community


Discussion on: The Power of Regular Expressions

hardik2310 profile image
gosai hardik

text = 'Some 42 number #12 more'
def mc = (text =~ /#(\d+)/)
println mc[0] // [#12, 12]
println mc[0][0] // #12
println mc[0][1] // 12

and what about this??

Thread Thread
awwsmm profile image
Andrew (he/him) Author • Edited

The only regular expression here is


which looks for a literal octothorpe # character, then captures () 1 or more digits \d+ which follow it.

The // surrounding the regular expression simply delimit the regular expression in Groovy, and the =~ says that we should look for matches to that regular expression within text. The result is assigned to mc.

So mc[0] contains the first match, which is a list of two elements: the entire matched expression #12, and the first capturing group 12.