Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

regex - 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 a colon (:) in regular expressions.

For example:

$pattern = '/^(([w]+:)?//)?(([dw]|%[a-fA-fd]{2,2})+(:([dw]|%[a-fA-fd]{2,2})+)?@)?([dw][-dw]{0,253}[dw].)+[w]{2,4}(:[d]+)?(/([-+_~.dw]|%[a-fA-fd]{2,2})*)*(?(&?([-+_~.dw]|%[a-fA-fd]{2,2})=?)*)?(#([-+_~.dw]|%[a-fA-fd]{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 greatly appreciated.

question from:https://stackoverflow.com/questions/21045297/use-of-colon-symbol-in-regular-expression

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 [-+_~.dw]:

  • - 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 =.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...