I'm looking for a regex pattern that I need for IIS. Basically I want it to match any directory path but reject file paths. I've searched all over with little luck.
Example: Match: /directory/content/css/
Match: /directory/content/css
Reject: /directory/content/css/main.css
Reject: /directory/content/css/main.anything
!--Due to feedback I've made some changes (Apologies this is my first time on the forum)--!
So far I've put together this pattern: ^(\/.*)(.*)*[^.*]$
It appears to start out ok accepting anything starting with / but it still accepts extensions.
Thanks
What about this
\/.*?[\w:]+
https://regex101.com/r/b4Y6Si/1
although it can leave / at the end
Regex:
(?:\..*(?!\/))+
This regex will match if you got a file path..
Regex101
Related
I want to extend my regexp for filepaths matching and I don't know how to do it even if I see the problem.
Innput example
"C://species/dinosaurs/trex.json"
Ouput example
["C://species/dinosaurs" "trex" "json"]
so that I have the folder path, the filename and the extension.
I also want the folder path to be optional
My regexp
I tried
"^(.*[\\\/])?(.*)\.(.*)$"
It outputs
["C://species/dinosaurs/" "trex" "json"]
Almost but I have the / at the end of the head
I so tried
"^((.*)[\\\/])?(.*)\.(.*)$"
I ouputs
["C://species/dinosaurs/" "C://species/dinosaurs" "trex" "json"]
Maybe better because I juste have to remove the first match whereas in the first case I have to post-process the string.
I see the problem because several / can exist in the body so that it is harder.
Is it possible to say that the end of the first matching group can be all but not /.
I tried
^(.*(?!\/))[\\\/]?(.*)\.(.*)$
Does not work. I just discovered negative assertions but the output is
["C://species/dinosaurs/trex" "json"]
Any clue ?
This one should suit your needs:
^(?:(.*)/)?([^/]+)\.([^.]+)$
Visualization by Debuggex
I'm using RegexBuddy and getting nowhere defining a search parameter for editpad.
I'm trying to search through my CMS web site for all instances of "http://" (to see where the protocol was hardcoded incorrectly), but every file has "http://particular.domain.com" in the comments near the top of the file.
How can I search for all EXCEPT those? This seems like it should be basic.
Here's your expression:
http:\/\/(?!particular\.domain\.com).+
Check out a demo here: https://regex101.com/r/eT2cX8/2
This portion is called a negative lookahead that lets you negate that match:
(?!particular\.domain\.com).+
use a negative lookahead:
'(?!http://particular.domain.com)http://'
is an example of a pattern that would match any http:// text EXCEPT the particular one
I need to match URL's which contain particular extentions (e.g. images). There are many examples available on the web, but they don't suit my needs entirely. The regex needs to match text where the scheme is included - e.g. http://img.izismile.com/img/img7/20140124/640/morning_picdump_490_640_07.jpg and also where the scheme is not included - e.g. www.google.co.uk/images/srpr/logo11w.png
The Regex I have come up with so far is
((?:(?:(?:http|ftp|gopher)\72\/\/)|(?:www|ftp)\.)S+\.(?:png|jpg))
But this doesn't appear to work - despite it looking correct in Debuggex
Can somebody help me correct my regex please.
You are missing a backslash on your S+ :)
((?:(?:(?:http|ftp|gopher)\72\/\/)|(?:www|ftp)\.)\S+\.(?:png|jpg))
I would like a regular expression to match an image format from a string(an url), but avoiding a concrete domain or directory.
For example:
"myImages/small/myImage.png"
"myImages/xxxx/myImage.png"
"myImages/large/myImage.png"
I would like a regexp to match any but not the 'large' one...
Many thanks in advance!
You want a negative lookahead assertion:
myImages\/(?!large\/).+\.(?:png|jpg|gif|jpeg|svg)$
The above will match any path that ends with one of those file extensions, but that does not have the text "large/" following "myImages/".
It's not very clear what your needs are, what output you want and what you can and cannot anchor against. If you edit your question to be more clear, you can get more-targeted information.
I use empty .hg_keep files to keep some (otherwise empty) folders in Mercurial.
The problem is that I can't find a working regex which excludes everything but the .hg_keep files.
lets say we have this filestructure:
a/b/c2/.hg_keep
a/b/c/d/.hg_keep
a/b/c/d/file1
a/b/c/d2/.hg_keep
a/b/.hg_keep
a/b/file2
a/b/file1
a/.hg_keep
a/file2
a/file1
and I want to keep only the .hg_keep files under a/b/.
with the help of http://gskinner.com/RegExr/ I created the following .hgignore:
syntax: regexp
.*b.*/(?!.*\.hg_keep)
but Mercurial ignores all .hg_keep files in subfolders of b.
# hg status
? .hgignore
? a/.hg_keep
? a/b/.hg_keep
? a/file1
? a/file
# hg status -i
I a/b/c/d/.hg_keep
I a/b/c/d/file1
I a/b/c/d2/.hg_keep
I a/b/c2/.hg_keep
I a/b/file1
I a/b/file2
I know that I a can hd add all the .hg_keep files, but is there a solution with a regular expression (or glob)?
Regexp negation might work for this. If you want to ignore everything except the a/b/.hg_keep file, you can probably use:
^(?!a/b/\.hg_keep)$
The parts of this regexp that matter are:
^ anchor the match to the beginning of the file path
(?! ... ) negation of the expression between '!' and ')'
a/b/\.hg_keep the full path of the file you want to match
$ anchor the match to the end of the file path
The regular expression
^a/b/\.hg_keep$
would match only the file called a/b/.hg_keep.
Its negation
^(?!a/b/\.hg_keep)$
will match everything else.
Not quite sure in what context you are using the Regex but this should be it, this matches all lines ending in .hg_keep:
^.*\.hg_keep$
EDIT: And here is a Regex to match items not matching the above expression:
^(?:(?!.*\.hg_keep).)*$
Try (?!.*/\.hg_keep$).
Looking for something similiar to this.
Found an answer, but it's not what we want to hear.
Limitations
There is no straightforward way to ignore all but a set of files. Attempting to use an inverted regex match will fail when combined with other patterns. This is an intentional limitation, as alternate formats were all considered far too likely to confuse users to be worth the additional flexibility.
Ref: https://www.mercurial-scm.org/wiki/.hgignore