I have the following URL:
https://comanage.example.edu/sp https://wiki.cs.example.org/sp https://intranet.math.example.edu/sp https://myapp.example.com/sp
For these URLs, I need to define a function to detect whether they are URLs and replace the https:// and sp paths from them. Basically, I just need the hostname. For example, as shown below:
https://comanage.example.edu/sp ->comanage.example.edu https://wiki.cs.example.org/sp ->wiki.cs.example.org https://intranet.math.example.edu/sp ->intranet.math.example.edu https://myapp.example.com/sp ->myapp.example.com
For non-URLs, this function should detect and not replace them. As follows:
nonurl.example.com -> ***no replacement***
Can anyone provide me with a solution to the above problem? I don't have much knowledge about using regular expressions.
Mode
^https?:\/\/
should be easy to use here. We can use it to replace any string starting withhttp://
andhttps://
with the empty stringIn a pattern, the
at the beginning^
symbol represents the beginning of a string. This means that ifhttp://
appears in the middle of the string, it will not match since it must be?
Marks the previous character as optional. In the pattern,s
is optional in order to findhttp
andhttps
\/
is required because slashes must be escaped