-4

I'm trying to figure out this regex that i use in PHP but i'm a nooby so is very difficult for me. Can you please explain me what does this regex do?

/(?:https?:\/\/)?(?:[a-zA-Z0-9.-]+?\.(?:[a-zA-Z])|\d+\.\d+\.\d+\.\d+)/

Thank you very much!

Weit
  • 1
  • 2
  • 1
    read [this](https://www.tutorialspoint.com/php/php_regular_expression.htm) – Gahan Dec 21 '17 at 11:57
  • So why do you use it? – TimBrownlaw Dec 21 '17 at 11:59
  • @TimBrownlaw I use it for validating a URL on my code, but i found it on internet and i wanna know exactly what it does. – Weit Dec 21 '17 at 12:06
  • You can use a **[graphical tool](https://regexper.com/#(%3F%3Ahttps%3F%3A%5C%2F%5C%2F)%3F(%3F%3A%5Ba-zA-Z0-9.-%5D%2B%3F%5C.(%3F%3A%5Ba-zA-Z%5D)%7C%5Cd%2B%5C.%5Cd%2B%5C.%5Cd%2B%5C.%5Cd%2B))** to explain your regex. – Federico Piazza Dec 21 '17 at 13:14

1 Answers1

0
Brackets

[0-9] - It matches any decimal digit from 0 through 9.
[a-z] - It matches any character from lower-case a through lowercase z.
[A-Z] - It matches any character from uppercase A through uppercase Z.
[a-Z] - It matches any character from lowercase a through uppercase Z.

Quantifiers

p+ - It matches any string containing at least one p. 
p* - It matches any string containing zero or more p's.     
p? - It matches any string containing zero or one p's.  
p{N} - It matches any string containing a sequence of N p's     
p{2,3} - It matches any string containing a sequence of two or three p's.   
p{2, } - It matches any string containing a sequence of at least two p's.
p$ - It matches any string with p at the end of it.
^p - It matches any string with p at the beginning of it.

Expression & Description

[[:alpha:]] - It matches any string containing alphabetic characters aA through zZ. 
[[:digit:]] - It matches any string containing numerical digits 0 through 9.
[[:alnum:]] - It matches any string containing alphanumeric characters aA through zZ and 0 through 9.   
[[:space:]] - It matches any string containing a space.

For more information view : Regex

MatHatrik
  • 762
  • 1
  • 6
  • 16