I am writing a nginx conf file and I would like to match a path that specifies a portion that should be treated as a variable. Suppose the root for the requests is /foo and within foo there are randomly named folders such as /foo/abd /foo/lkd however every randomly named folder contains a static named folder /images. I would like my regex to designate that second part of the URL as a variable. Something like /foo/{{variable}}/images
To match /foo/{{variable}}/images where {{variable}} is a string having multiple characters from range a-z use following regex.
Regex: /\/foo\/([a-z]+)\/images/
Explanation: It matches multiple characters between /foo/ and /images
Regex101 Demo
Related
Need help from regex gurus. I need a regex pattern to match relative file paths within the source-code of Javascript files.
Must match only relative paths that have at least 1 or more / and a file extension.
Example paths in source:
SHOULD MATCH:
var p = "./path/to/file.jpg";
var paths ['/path/file.jpg', `./sample.jpg`]
ONLY needs to match after "|' up to last /.
So, from above paths, this is what the matched
groups should find:
"./path/to/"
"/path/"
"./"
I plan on using the regex to replace the relative paths so the source only points to the file.
SHOULD NOT MATCH ANY OF THESE THAT LOOK LIKE PATHS:
var mime = "text/html";
var reg = /some\-regex/;
var replaced = "test.match(/[a-z]/, "/")
Something like this could work:
(?<=(['"`]))\.?\/.*?(?=\.[^.]+\1)
(?<=(['"`])) - find an apostrophe, double-quote, or backtick and store it in capture group #1
\.?\/ - optional period followed by a slash
.*? - loosely continue to capture everything
(?=\.[^.]+\1) - close with a file extension and the same string opening as capture group #1
https://regex101.com/r/raEARx/1
I am trying to figure out how to pull the following string out of a folder path... I want to pull COMPANY_NAME from the below folder path. Is there a way to use REGEX to pull string between 2nd and 3rd backslash?
Example:
\10.20.3.23\S$\COMPANY_NAME\Main_5e08a942f39a430db0b081736a3f1881\C_VOL-b002.spf
Try this (?(DEFINE)(?<urlPart>[^\\\s]+))\\\\(?&urlPart)\\(?&urlPart)\\\K(?&urlPart) demo
It will match the desired part of the URL you are after. Things to note:
The url does not need to start at the beginning of the string (if you require this add ^ after the define group)
It will match many urls in the same string
It will match even if there is no file name
White space will invalidate the match
See the demo for details
If you were wondering it uses subroutine definitions to reuse parts of the regex.
I'm trying to add something along the lines of this regex logic.
For Input:
reading/
reading/123
reading/456
reading/789
I want the regex to match only
reading/123
reading/456
reading/789
Excluding reading/.
I've tried reading\/* but that doesn't work because it includes reading/
You must escape your backslashes in Hugo, \\/\\d+.
I need a Regular Expression that can match url with uppercase letters but do not match if it contains a filename like .jpg,.css,.js etc
I want to redirect all uppercase url to lowercase but only when it is not pointing to a file resource.
Try using a regex visualizer like regexpal.com.
Here's an example of a regular expression that approximates what you're trying to do:
\w+\.(?:com|net)(?:/[A-Z]+){1,}[/]?(?:\.jpg|\.png|\.JPG|\.PNG){0}$
\w+\.(?:com|net) captures a domain of the form word.com or word.net. (You'll need to add other domains or improve this if you want to capture subdomains as well.)
(?:/[A-Z]+){1,}[/]?captures all-caps directories like /FOO/BAR/ with an optional trailing slash.
(?:\.jpg|\.png|\.JPG|\.PNG){0}$ captures exactly zero of the extensions listed; you'll obviously need to add to this list of extensions.
But perhaps rethink your routing; it's better form to keep all assets in devoted directories on your server, so that you can simply pass any request to mysite.com/assets/ along unchanged while handling other URLs.
I need a regular expression for IIS URL Rewrite that will process the rule only when the expression matches any bit of the URL EXCEPT a specific sub-root directory.
Example:
www.mysite.com/wordpress - process rule on any URL that starts with /wordpress after the domain name
www.mysite.com/inventory - do not process rule on any URL that starts with /inventory after the domain name
Tried .*(?<!^\/inventory\/.*) but it still matches the entire string.
You need a lookahead rather than lookbehind. Something like this I think:
^([^/]*/){1}(?!inventory\b)
Where you change 1 to 2 when the exclusion is needed at the next lower sublevel, etc.