I can confirm the (?i) at the beginning of the regex makes it case insensitive.

Anyway, if your purpose is to reduce the regex length you can use the groovy dollar slashy string form. It allows you to not escape slashes / (the escape char becomes $).

In addition:

  • the POSIX chars \p{Alnum} is the compact equivalent of [0-9a-zA-Z] (this way you can avoid to use the (?i) at all).

  • remove unneeded backslashed dash from char class [\-\.] -> [-.] (it's not mandatory when the dash is the first or the last element and also the dot is always literal inside a character group).

  • remove unneeded round brackets from the protocol section

In the following version I take advantage of the multiline support of dollar slashy string and the free-spacing regex flag (?x):

$/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round brackets
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [a-zA-Z0-9])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

A dollar-slashy compact version:

$/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}([1-9][0-9]{0,4})?(/\S*)?/$

If you must use the slashy version this is an equivalent:

/^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

A snippet of code to test all these regex:

def multiline_pattern = $/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round bracket
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char, instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [0-9a-zA-Z])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

def compact_pattern = $/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(/\S*)?/$

def slashy_pattern  = /^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

def url1    = 'https://www.example-test.domain.com:12344/aloha/index.html'
def notUrl1 = 'htxps://www.example-test.domain.com:12344/aloha/index.html'
def notUrl2 = 'https://www.example-test.domain.com:02344/aloha/index.html'

assert url1 ==~ multiline_pattern
assert url1 ==~ compact_pattern
assert url1 ==~ slashy_pattern

assert !( notUrl1 ==~ compact_pattern )
assert !( notUrl1 ==~ slashy_pattern  )
assert !( notUrl1 ==~ slashy_pattern  )

assert !( notUrl2 ==~ compact_pattern )
assert !( notUrl2 ==~ slashy_pattern  )
assert !( notUrl2 ==~ slashy_pattern  )
Answer from Giuseppe Ricupero on Stack Overflow
Top answer
1 of 2
25

I can confirm the (?i) at the beginning of the regex makes it case insensitive.

Anyway, if your purpose is to reduce the regex length you can use the groovy dollar slashy string form. It allows you to not escape slashes / (the escape char becomes $).

In addition:

  • the POSIX chars \p{Alnum} is the compact equivalent of [0-9a-zA-Z] (this way you can avoid to use the (?i) at all).

  • remove unneeded backslashed dash from char class [\-\.] -> [-.] (it's not mandatory when the dash is the first or the last element and also the dot is always literal inside a character group).

  • remove unneeded round brackets from the protocol section

In the following version I take advantage of the multiline support of dollar slashy string and the free-spacing regex flag (?x):

$/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round brackets
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [a-zA-Z0-9])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

A dollar-slashy compact version:

$/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}([1-9][0-9]{0,4})?(/\S*)?/$

If you must use the slashy version this is an equivalent:

/^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

A snippet of code to test all these regex:

def multiline_pattern = $/(?x)
  ^                      # start of the string
  https?://              # http:// or https://, no need of round bracket
  (                      # start group 1, have to be a non capturing (?: ... ) but is less readable
    \p{Alnum}+           # one or more alphanumeric char, instead of [a-zA-Z0-9]
    ([.-]\p{Alnum}+)*    # zero or more of (literal dot or dash followed by one or more [0-9a-zA-Z])
    \.                   # a literal dot
  )+                     # repeat the group 1 one or more
  \p{Alpha}{2,40}        # between 2 and 40 alphabetic chars [a-zA-Z]
  (:[1-9][0-9]{0,4})?    # [optional] a literal colon ':' followed by at least one non zero digit till 5 digits
  (/\S*)?                # [optional] a literal slash '/' followed by zero or more non-space chars
/$

def compact_pattern = $/^https?://(\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(/\S*)?/$

def slashy_pattern  = /^https?:\/\/(?:\p{Alnum}+([.-]\p{Alnum}+)*\.)+\p{Alpha}{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/

def url1    = 'https://www.example-test.domain.com:12344/aloha/index.html'
def notUrl1 = 'htxps://www.example-test.domain.com:12344/aloha/index.html'
def notUrl2 = 'https://www.example-test.domain.com:02344/aloha/index.html'

assert url1 ==~ multiline_pattern
assert url1 ==~ compact_pattern
assert url1 ==~ slashy_pattern

assert !( notUrl1 ==~ compact_pattern )
assert !( notUrl1 ==~ slashy_pattern  )
assert !( notUrl1 ==~ slashy_pattern  )

assert !( notUrl2 ==~ compact_pattern )
assert !( notUrl2 ==~ slashy_pattern  )
assert !( notUrl2 ==~ slashy_pattern  )
2 of 2
6

You place them in the regexp - like in java:

groovy:000> "http://example.COM" ==~ /^(https?:\/\/)(?:[a-z0-9]+([\-\.][a-z0-9]+)*\.)+[a-z]{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/
===> false
groovy:000> "http://example.COM" ==~ /^(?i)(https?:\/\/)(?:[a-z0-9]+([\-\.][a-z0-9]+)*\.)+[a-z]{2,40}(:[1-9][0-9]{0,4})?(\/\S*)?/
===> true
🌐
TutorialsPoint
tutorialspoint.com › groovy › groovy_equalsignorecase.htm
Groovy - String equalsIgnoreCase() method
equalsIgnoreCase() method compares this String to another String, ignoring case considerations. This method returns true if the argument is not null and the Strings are equal, ignoring case; false otherwise.
🌐
FileBot
filebot.net › forums › scripting and automation
Groovy =~ operator case insensitive? - FileBot
October 5, 2018 - I am trying to use the groovy find operator like this: ... f =~ /DVDR|DVD5|DVD 5|DVD9|DVD 9/ ? 'DVD/movies' But I am finding that this does a case-sensitive search. Is there a way to make this case-insensitive? ... You can add (?i) at the beginning of your regex to turn on the case-insensitive flag...
🌐
Mrhaki
blog.mrhaki.com › 2009 › 09 › groovy-goodness-using-regular.html
Groovy Goodness: Using Regular Expression Pattern Class - Messages from mrhaki
September 11, 2025 - // Easy for switch and grep statements. def p = ~/\w+vy/ assert p.isCase('groovy') switch ('groovy') { case ~/java/: assert false; break; case ~/gr\w{4}/: assert true; break; default: assert false } // We can use flags in our expressions. In this sample // we use the case insensitive flag (?i).
Author: NCEI-NOAAGov
🌐
Unogs
eth.unogs.com › groovy-case-insensitive-regex-match
Groovy Case Insensitive Regex Match​ Quick and Easy Solution
Regex - Grails/Groovy Regular Expression- How To Use (?i ... ... Dr. Brisa Kub Jr. Alabama Contributor ... best www.ngdc.noaa.gov In Groovy, this match flag is "(?x)" and can be combined with other flags you wish to turn on such as "(?ix)" for both extended and case-insensitive modes.
🌐
Wjw465150
wjw465150.github.io › blog › Groovy › my_data › Goodness › Regular-Using Regular Expression Pattern Class.htm
Groovy Goodness: Using Regular Expression Pattern Class
To define a regular expression pattern in Groovy we can use the tilde (~) operator for a String. The result is a java.util.regex.Pattern object. The rules to define the pattern are the same as when we do it in Java code. We can invoke all standard methods on the Pattern object.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › groovy › groovy_comparetoignorecase.htm
Groovy - String compareToIgnoreCase() method
compareToIgnoreCase() method is used to compare two strings lexicographically, ignoring case differences. If strings are same, 0 is returned else a negative value is returned. str − string value for comparison.
🌐
Szymon Stepniak
e.printstacktrace.blog › home › groovy cookbook › groovy regular expressions - the definitive guide (part 1)
Groovy Regular Expressions - The Definitive Guide (Part 1)
May 8, 2020 - Groovy makes working with regex very simple, thanks to the find operator (=~), exact match operator (==~), or slashy strings (e.g. /\d+\.\d+\.\d+/) that make writing regular expressions as simple as possible.
🌐
Andrey Hihlovskiy
akhikhl.wordpress.com › 2013 › 07 › 31 › power-of-switch-statement-in-groovy
Power of switch statement in groovy | Andrey Hihlovskiy
July 31, 2013 - Professional blog on groovy, gradle, Java, Javascript and other stuff. Leave a comment Posted by akhikhl on July 31, 2013 ... def x = 'test' switch(x) { case null: println 'null!' break case ~/(?i)Test/: println 'got it!' break default: println 'something else' } here second ‘case’ does case-insensitive regex comparison.
🌐
DZone
dzone.com › data engineering › data › groovy goodness: remove part of string with regular expression pattern
Groovy Goodness: Remove Part of String With Regular Expression Pattern
November 23, 2013 - The first match found is replaced with an empty String. In the following sample code we see how the first match of the pattern is removed from the String: // Define regex pattern to find words starting with gr (case-insensitive).
🌐
Bennadel
bennadel.com › blog › 301-case-insensitive-java-regular-expressions-how-did-i-miss-that.htm
Case Insensitive Java Regular Expressions - How Did I Miss That!?!
March 22, 2020 - How did I miss that? I have been doing regular expressions for a long time now and this one has escaped me. Crazy! But, it's so awesome. You just put the flag (?i) in your regular expression and everything to the right of it will be case-insensitive:
🌐
Rexegg
rexegg.com › regex-modifiers.php
What are regex modifiers, and how to turn them on?
Rather than repeatedly explain what they do and the multiple ways to turn them on in every regex flavor, I decided to gather the four common ones (i, s, m and x) in one place. The final section briefly surveys other modifiers, which are usually language-specific. Jumping Points For easy navigation, here are some jumping points to various sections of the page: ✽ Case Insensitivity: i ✽ DOTALL (Dot Matches Line Breaks): s (except Ruby and JavaScript) ✽ Multiline (^ and $ Match on Every Line): m (except Ruby) ✽ Free-Spacing: x (except JavaScript) ✽ Other Modifiers ✽ PCRE's Special Start-of-Pattern Modifiers (direct link)
🌐
Mrhaki
blog.mrhaki.com › 2013 › 11 › groovy-goodness-remove-part-of-string.html
Groovy Goodness: Remove Part of String With Regular Expression Pattern - Messages from mrhaki
November 18, 2013 - The first match found is replaced with an empty String. In the following sample code we see how the first match of the pattern is removed from the String: // Define regex pattern to find words starting with gr (case-insensitive).
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-how-case-insensitive-search-string-matches-method
Java: How to perform a case-insensitive search using the String ‘matches’ method | alvinalexander.com
September 30, 2019 - Solution: Use the String matches method, and include the magic (?i:X) syntax to make your search case-insensitive. (Also, remember that when you use the matches method, your regex pattern must match the entire string.)