use of colon symbol in regular expression


I am new to regex .I am studying it in regularexperssion.com ..The question is that i need to know what is the use of colon (:) in regular expressions ..

For example ..:

$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';

which matches

$url1  = "http://www.somewebsite.com";
$url2  = "https://www.somewebsite.com";
$url3  = "https://somewebsite.com";
$url4  = "www.somewebsite.com";
$url5  = "somewebsite.com";

Yeah any help would be greately appreciated ..:)


Answers:


Colon : is simply colon. It means nothing, except special cases like, for example, clustering without capturing (also known as a non-capturing group):

(?:pattern)

Also it can be used in character classes, for example:

[[:upper:]]

However, in your case colon is just a colon.

Special characters used in your regex:

In character class [-+_~.\d\w]:

  • - means -
  • + means +
  • _ means _
  • ~ means ~
  • . means .
  • \d means any digit
  • \w means any word character

These symbols have this meaning because they are used in a symbol class []. Without symbol class + and . have special meaning.

Other elements:

  • =? means = that can occur 0 or 1 times; in other words = that can occur or not, optional =.