I have one path in form of string like this Folder1/File.png
But in this string sometimes if file is hidden or folder is hidden I don't want it to be matched by my regex.
regex = %r{([a-zA-Z0-9_ -]*)\/[^.]+$}
input_path = "Folder_1/.file" # This shouldn't be matched.
input_path = "Folder/file.png" # This should be matched.
But my regex works for first input but its not even matching second one.
You are currently looking for \\/[^.]+$
, that is a /
followed by any character except .
until the end. Since the filename+extension format has a .
character, it fails to match the second case.
Instead of using [^.]+$
, check only that the character following /
is not .
, and match everything after that:
([a-zA-Z0-9_ -]*)\/[^.].*$
While there are some suggestions here that work, my suggestion would be
\/[^.][^\/\n]+$
It finds a slash , followed by anything but a dot, which in turn is followed by one, or more, of anything but a slash or a newline
.
To handle the two lines given as an example,
Folder_1/.file
Folder/file.png
it takes 8 steps.
The suggested ones all work, but ([a-zA-Z0-9_ -]*)\\/[^.]
takes 75 steps, ([a-zA-Z0-9_ -]*)\\/[^.]+\\.[^.]+\\z
78 steps and ([a-zA-Z0-9_ -]*)\\/[^.].*$
takes 77 steps.
This may be totally irrelevant and I may have missed some angle, but I wanted to mention it ;)
regex = %r{([a-zA-Z0-9_ -]*)\/[^.]}
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.