DEV Community

loading...

Discussion on: The Power of Regular Expressions

Collapse
hardik2310 profile image
gosai hardik

code-maven.com/groovy-regex

i am not unerstanding this link's RE , can you explain please ??

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

This one?

^https?://([^/]*)/([^/]*)$

Here's an explanation:

^http   -- all desired URLs must begin with http
     s? -- followed by (optionally) an 's' (http or https)
://     -- followed by the "://" which defines the http/s protocol

(       -- the first capturing group contains
  [^/]* -- any number (including zero) non-/ characters
)
/       -- followed by a slash
(       -- the second capturing group contains
  [^/]* -- any number (including zero) non-/ characters
)$      -- and must be followed by the end of the line

So we have an "http://" or an "https://", followed by the first capturing group, which is everything before the first "/". The second capturing group is everything after the first slash, so:

https://www.myexample.com/secondpart
        ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^
        1st capturing grp 2nd capturing group 
Collapse
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

#(\d+)

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.