How to add comments in regex?
We can explain our regular expression within the expression using comments.
Syntax:(?#Commented Code)
Suppose we have a two strings below:
Hello world
hello World
Regex to match these two lines without comments:
(?i)(hello)\s(world)
Regex to match these two lines with comments:
(?#It is a case insensitive flag)(?i)(?#Will match 'hello')(hello)(?#Matches any whitespace character like space, tab, newline etc)\s(?#Will match 'world')(world)
(?#It is a case insensitive flag)
and others with (?#...)
are comments in this regular expression and does not affect pattern matching.
You can test these regular expressions in the link : https://regex101.com/