简体   繁体   中英

RegEx to match the patterns

I've got a requirement

.domain
.domain.com
.domain.com/path
.domain.com:443/path
domain.com
domain.com/path
domain.com:443/path
domain
/path
:443/path

Should all be true

However

.
./path
.:443
*.domain
*.domain/path
*.domain:443/path
/
:443
*/path
anything with ? in it

should all return false.

I came up with the below in JS. But, after a few modifications I am completely lost..

/^\.?[^:\/]([\da-z\.-]+)\.?([a-z\.]{2,6})?(:[0-9]+)?([\/\w \.-]*)*\/?$/ 

My test stub looks below:

console.log(urlRegExp('*.domain/path'));
console.log(urlRegExp('*.domain:443/path'));
console.log(urlRegExp('/'));
console.log(urlRegExp(':443'));
console.log(urlRegExp('*/path'));
console.log(urlRegExp('domain.com?q=a'));

function urlRegExp(){ 
  return /^\.?[^:\/]([\da-z\.-]+)\.?([a-z\.]{2,6})?(:[0-9]+)?([\/\w \.-]*)*\/?$/.test(arguments[0]) + " " + arguments[0];  
} 

I am unable to handle all the listed strings with this pattern. Whenever I change something to make something pass, another string fails. I am a beginner and just started into understanding RegEx deeply with the cookbook. However, it might take a while and I need it to be done sooner. Any help or guidance would be awesome.

Current Results:

should return true:

true .domain
true .domain.com
true .domain.com/path
true .domain.com:443/path
true domain.com
true domain.com/path
true domain.com:443/path
true domain
false /path  -- should be true
false :443/path  -- should be true

should return false:

false . 
false ./path
false .:443
true *.domain -- should be false
true *.domain/path -- should be false
true *.domain:443/path -- should be false
false /
false :443
false */path
false domain.com?q=a

Maybe something like this...

/^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/

Explained here...

/^                 # Start regex, and start matching
[./:a-z]           # starts with dot, slash, colon or a-z
([0-9]+\/)?        # optionally has multi-digit number followed by slash
[a-z]+             # has one or more letters next
[^?]*              # has zero or more characters that are not `?`
$/                 # end of matching and end regex

Hard to know the specifics of what you are aiming for

Tests in...

Coffeescript

rex = /^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/

for str in """
.domain
.domain.com
.domain.com/path
.domain.com:443/path
domain.com
domain.com/path
domain.com:443/path
domain
/path
:443/path
""".split /[\r\n]+/
    console.log "Should be true - is #{if str.match rex then 'true ' else 'false'} #{str}"

for str in """
.
./path
.:443
*.domain
*.domain/path
*.domain:443/path
/
:443
*/path
domain.com?
domain.?com
?domain.com
""".split /[\r\n]+/
    console.log "Should be false - is #{if str.match rex then 'true ' else 'false'} #{str}"

and in...

Javascript

var rex, str, _i, _j, _len, _len1, _ref, _ref1;

rex = /^[./:a-z]([0-9]+\/)?[a-z]+[^?]*$/;

_ref = ".domain\n.domain.com\n.domain.com/path\n.domain.com:443/path\ndomain.com\ndomain.com/path\ndomain.com:443/path\ndomain\n/path\n:443/path".split(/[\r\n]+/);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  str = _ref[_i];
  console.log("Should be true - is " + (str.match(rex) ? 'true ' : 'false') + " " + str);
}

_ref1 = ".\n./path\n.:443\n*.domain\n*.domain/path\n*.domain:443/path\n/\n:443\n*/path\ndomain.com?\ndomain.?com\n?domain.com".split(/[\r\n]+/);
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  str = _ref1[_j];
  console.log("Should be false - is " + (str.match(rex) ? 'true ' : 'false') + " " + str);
}

Both output the same...

Should be true - is true  .domain
Should be true - is true  .domain.com
Should be true - is true  .domain.com/path
Should be true - is true  .domain.com:443/path
Should be true - is true  domain.com
Should be true - is true  domain.com/path
Should be true - is true  domain.com:443/path
Should be true - is true  domain
Should be true - is true  /path
Should be true - is true  :443/path
Should be false - is false .
Should be false - is false ./path
Should be false - is false .:443
Should be false - is false *.domain
Should be false - is false *.domain/path
Should be false - is false *.domain:443/path
Should be false - is false /
Should be false - is false :443
Should be false - is false */path
Should be false - is false  domain.com?
Should be false - is false  domain.?com
Should be false - is false ?domain.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM