I need to know if a string contains a specific domain
I have an array like this
private var validDomain:Array = new Array(
"http://*.site1.com",
"http://*.site2.com",
"http://*.site3.com",
);
private var isValidDomain:Boolean = false;
private var URL:string = "http://mysub.site2.com";
now i would check if my string is valid, so i think something like that:
for each (var domain_ in validDomain){
if(SOMEREGEX){
isValidDomain=true;
}
}
What i put in SOMEREGEX?!
The problem lies in the fact that you use two different logics.
The first one is a wildcard-based string, with wildcards like *; you must translate this wildcard based pattern in a regular expression pattern.
To accomplish this, a quick and dirty solution would be to do some string replacements like:
substituting the * wildcard in the pattern with .*
escaping the characters in the wildcard based pattern that are "special" to regular expressions (e.g. substituting . with \.
With this logic in mind, you will transform a wildcard based pattern like:
http://*.mydomain.com
into a regular expression pattern:
http:\/\/.*\.mydomain\.com
which you can use to test your string.
edit: .* is a very crude way to test for a subdomain, to do things neatly you should use a correct pattern like the ones in this thread: Regexp for subdomain
Related
I am using NextAuth for Next.js for session management. In addition, I am using the middleware.js to protect my routes from unauthenticated users.
According to https://nextjs.org/docs/advanced-features/middleware#matcher,
if we want to exclude a path, we do something like
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - static (static files)
* - favicon.ico (favicon file)
*/
'/((?!api|static|favicon.ico).*)',
],
}
In this example, we exclude /api, /static,/favicon.icon. However, I want to exclude all path except the home page, "/". What is the regular expression for that? I am tried '/(*)'. It doesn't seem to work.
The regular expression which matches everything but a specific one-character string / is constructed as follows:
we need to match the empty string: empty regex.
we need to match all strings two characters long or longer: ..+
we need to match one-character strings which are not that character: [^/].
Combining these three together with the | branching operator: "|..+|[^/]".
If we are using a regular expression tool that performs substring searching rather than a full match, we need to use its anchoring features; perhaps it supports the ^ and $ notation for that: "^(|..+|[^/])$".
I'm guessing that you might not want to match empty strings; in which case, revise your requirement and drop that branch from the expression.
Suppose we wanted to match all strings, except for a specific fixed word like abc. Without negation support in the regex language, we can use a generalization of the above trick.
Match the empty string, like before, if desired.
Match all one-character strings: .
Match all two-character strings: ..
Match all strings longer than three characters: ....+
Those simple cases taken care of, we focus on matching just those three-symbol strings that are not abc. How can we do that?
Match all three-character strings that don't start with a: [^a]...
Match all three-character strings that don't have a b in the middle: .[^b].
Match all three-character strings that don't end in c: ..[^c].
Combine it all together: "|.|..|....+|[^a]..|.[^b].|..[^c]".
For longer words, we might want to take advantage of the {m,n} notation, if available, to express "match from zero to nine characters" and "match eleven or more characters".
I will need to exclude the signin page and register page as well. Because, it will cause an infinite loop and an error, if you don't exclude signin page. For register page, you won't be able to register if you are redirected to the signin page.
So the "/", "/auth/signin", and "/auth/register" will be excluded. Here is what I needed:
export const config = {
matcher: [
'/((?!auth).*)(.+)'
]
}
I'm trying to match a route that has the keywords -episode or -movie.
Such as /steins-gate-episode-1 or /pokemon-movie-10
I tried doing this:
$app->get('/{slug:episode|movie}', \App\Controller\EpisodeController::class . ':getBySlug');
But it isn't matching.
Any help would be appreciated. I am completely new to this btw.
Your regular expression matches only the exact strings "episode" and "movie". If you want to check if the URL contains the substrings, you can use this:
$app->get('/{slug:.*episode.*|.*movie.*}', function ($request, $response, $args) {
echo $args['slug'];
});
.* means "any number of any character" . Sure are there more advanced regexp patterns but that will do what you need.
We are currently matching "service_hub*queue"
I want to ignore the case "service_hub_scout_dead_queue" and yet still match everything else.
What is the regular expression for that ?
This javascript sollution gives an array with the matches
var myText = 'service_hub_anything_queue Add service_hub_scout_dead_queue something service_hub_someting_queue else';
var myMatches = myText.match(/service_hub(?!_scout_dead_)\w+queue/g);
If you are rather interested in what follows a match
var mySplit = ('dummy'+myText).split(/service_hub(?!_scout_dead_)\w+queue/g).filter(function(txt,i) {return (i>0);})
I put 'dummy' and then filter away the first part to make it work both if the sting starts with a valid tag and when it does not.
Using negative lookbehind: "service_hub_.*?(?<!_scout_dead)_queue"
This appears to be widely supported by popular regex engines; I've tested with Java (or Scala, rather) just to make sure it works.
I have to split URIs on the second portion:
/directory/this-part/blah
The issue I'm facing is that I have 2 URIs which logically need to be one
/directory/house-&-home/blah
/directory/house-%26-home/blah
This comes back as:
house-&-home and house-%26-home
So logically I need a regex to retrieve the second portion but also remove everything between the hyphens.
I have this, so far:
/[^(/;\?)]*/([^(/;\?)]*).*
(?<=directory\/)(.+?)(?=\/)
Does this solve your issue? This returns:
house-&-home and house-%26-home
Here is a demo
If you want to get the result:
house--home
then you should use a replace method. Because I am not sure what language you are using, I will give my example in java:
String regex = (?<=directory\/)(.+?)(?=\/);
String str = "/directory/house-&-home/blah"
Pattern.compile(regex).matcher(str).replaceAll("\&", "");
This replace method allows you to replace a certain pattern ( The & symbol ) with nothing ""
I am not really good at writing regular expressions but i need to match some parts of a URL.
http://example.com/service-page/service1/sub-service2/other-service/
my attempt looks like
service-page/(.?.+?)$
and the capture looks like service1/sub-service2/other-service/
but i need only to match if there is constant service-page inside the url and to capture the last part of the url other-service
http://example.com/[service-page]/service1/sub-service2/[other-service]/
I have to confess that I don't have WP to test with, but based on working with other regex engines, I don't see why this shouldn't work:
service-page/.*?([^/]+)/?$
To capture the name of the last subfolder:
ServicePageSubFolder("http://example.com/service-page/service1/sub-service2/other-service/");
public string ServicePageSubFolder(string url)
{
Regex regex = new Regex(#"service-page.*/([^/]+)/?");
Match match = regex.Match(url);
if (match.Success)
return match.Groups[1].Value;
return null;
}
Good luck with your quest.