This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I am looking for it about 2 hours, but can not find what I need.
what I need is very simple:
allow: google.com, http://google.com, https://google.com
disallow spaces "goo gle.com"
with a valid domain: I mean it should have a dot "." + any domain (.com, .net etc.)
and allow anything after that: "googl.com/dsfsdf/sdfs/blablahblah/" without spaces
thanks
Edit:
Thanks all, I had to write it myself.
if (!/^((ftp|http|https):\/\/)?([a-z0-9_\.-]+)\.{1}([a-z0-9_\/\?\=\-\%-]+)$/.test(uri)
|| /([\._\/\?\=\-\%-])\1/.test(uri)) {
}
ps: I am noob in regexs.
www.google.com
http://www.google.com
mailto:somebody#google.com
somebody#google.com
www.url-with-querystring.com/?url=has-querystring
The REGEX below matches all the above cases
((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)
REGEX Explanation can be found here
Working Example
Something that's working for me on a production product (haven't received any complaints yet):
((www\.|(http|https|ftp|news|file)+\:\/\/)?[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])
Related
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 2 years ago.
I want to validate an email field using regex in such a way that my email has to has #moore in it.
like a#moore.af, b#moore.sg, and so on. how can I write its pattern? I am using typescript and angular reactive form.
Your help is much appreciated.
You can try to use ([\w-\.]+#moore\.[\w+]{1,5}) to match an email address, as I left a 1-5 characters' space for the domain name.
In JavaScript flavour: const regex = /([\w-\.]+#moore\.[\w+]{1,5})/gm; then you can use regex.test(str) to validate the email field.
Edit:
As #Toto pointed out, This regex matches .....#moore.++++. Better regex would be:
([a-zA-Z0-9\.-]+#moore\.[a-zA-Z0-9\.]{1,5})
to only accept alphabet/number in the domain name.
This question already has answers here:
How does IIS URL Rewrite handle # anchor tags
(1 answer)
How to get Url Hash (#) from server side
(6 answers)
Closed 2 years ago.
I can't get IIS to match a URL with a #. Instead It trims the URL at the #
/product name to match #1.html
^seeds/([A-Za-z0-9-_ #]+).html/?$
I've tried escaping it, to no avail.
Result from IIS is /product name to match
Any ideas?
N.B. whoever marked my question as answered elsewhere please read my question again.
This question already has answers here:
Parsing valid parent directories with regex
(3 answers)
Closed 8 years ago.
Given a string like "/foo/bar/baz/quux" (think of it like a path to a file on a unixy system), how could I (if at all possible) formulate a regular expression that gives me all possible paths that can be said to contain file quux?
In other words, upon running a regexp against the given string ("/foo/bar/baz/quux"), I would like to get as results:
"/foo/"
"/foo/bar/"
"/foo/bar/baz/"
I've tried the following:
'/\/.+\//g' - this is greedy by default, matches "/foo/bar/baz/"
'/\/.+?\//g' - lazy version, matches "/foo/" and also "/baz/"
P.S.: I'm using Perl-compatible Regexps in PHP in function preg_match(), for that matter)
Felipe not looking for /foo/bar/baz, /bar/baz, /baz but for /foo, /foo/bar, /foo/bar/baz
One solution building on regex idea in comments but give the right strings:
reverse the string to be matched: xuuq/zab/rab/oof/ For instance in PHP use strrev($string )
match with (?=((?<=/)(?:\w+/)+))
This give you
zab/rab/oof/
rab/oof/
oof/
Then reverse the matches with strrev($string)
This give you
/foo/bar/baz
/foo/bar
/foo
If you had .NET not PCRE you could do matching right to left and proably come up with same.
This solution will not give exact output as you are expecting but still give you pretty useful result that you can post-process to get what you need:
$s = '/foo/bar/baz/quux';
if ( preg_match_all('~(?=((?:/[^/]+)+(?=/[^/]+$)))~', $s, $m) )
print_r($m[0]);
Working Demo
OUTPUT:
Array
(
[0] => /foo/bar/baz
[1] => /bar/baz
[2] => /baz
)
Completely different answer without reversing string.
(?<=((?:\w+(?:/|$))+(?=\w)))
This matches
foo/
foo/bar/
foo/bar/baz/
but you have to use C# which use variable lookbehind not PCRE
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
i am writing a regular expression to check a website URL it should check the following scenarios:
pass:
- www.example.com
- example.com
- www.example.com/something
- example.com/something
and prevent every other urls
its working perfectly for every thing except one case (www.example), how can i handel this case
"www.example" must not pass
my regular expression :
^[a-zA-Z0-9][a-zA-Z0-9]+([.][a-zA-Z0-9]+)+(/.*)?$
can any one help please ?
Thanx.
Heres the best i could get
(www.){1}[a-zA-Z0-9]+[.]{1}[\w]+[/\w]*
Result
www.example.com - true
www.example.com/ - true
www.example.com/xyx - true
www.example.com/xy/s/ - true
www.example. - False
www.example - False
please note that this wont accept 'example.com'
Tested # http://gskinner.com/RegExr/
Try this one:
^(www\.)?(?!www)[a-zA-Z0-9]+\.[a-zA-Z]{2,6}/?[a-zA-Z0-9]+$
This is the actual URL validating regex used in Django 1.5.1:
import re
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
This does both ipv4 and ipv6 addresses as well as GET parameters.
Found in the code here, Line 44.
Try this:
_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS
I can't claim credit though; I yanked it from here:
http://mathiasbynens.be/demo/url-regex
They've got a reasonable chart with lots of expressions with pass/fail for each case against each expression.
Not the best regex but works in many cases:
^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}(/.*)*$
Edit:
^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+(com|org|info|biz|us)/?([^/]*)$
To allow trailing slash:
^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+(com|org|info|biz|us)/?([^/]*)/?$
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
The Hostname Regex
I'm trying to use pcrepp (PCRE) to extract hostname from url.
the pcre regular expression is as same as Perl 5 regular expression.
for example:
url = "http://www.pandora.com/#/volume/73";
// the match will be "http://www.pandora.com/".
I can't find the correct syntax of the regex for this example.
Needs to work for any url: amazon.com/sds/ should return: amazon.com.
or abebooks.co.uk/isbn="62345627457245"/blabla/ should return abebooks.co.uk
I don't need to check if the url is valid. just to get the hostname.
Something like this:
^(?:[a-z]+://)?[^/]+/?
See Regexp::Common::URI::http which uses sub-patterns defined in Regexp::Common::URI::RFC2396. Examining the source code of those modules should give you a good idea how to put together a decent pattern.
Here is one possibility:
^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$
And another:
^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
These and other URL related regular expressions can be found here: Regular Expression Library
string regex1, regex2, finalRegex;
regex1 = "^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?#)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??";
regex2 = "([^#]+)?#?(\\w*)";
//concatenation
finalRegex= regex1+regex2;
the result will be at the sixth place.
answered in another question I asked: Details.