Matching a specific string between two forward slashes
I need to find开发者_高级运维 all %20's between two forward slashes.
example.com/book%20/picture1.jpg
example.com/book%20/picture2.jpg
example.com/book%20/picture3.jpg
example.com/pages/picture%20book1.jpg
example.com/pages/picture%20book2.jpg
example.com/pages/picture%20book3.jpg
It should find the first 3 links, not the last 3.
I cant seem to figure it out.
This fulfills the requirement "%20" between two forward slashes
/\/[^/]*%20[^/]*\//
\/ literal "/"
[^/]* every character except for a slash, zero or more occurences
%20 literal "%20"
[^/]* every character except for a slash, zero or more occurences
\/ literal "/"
Such an RE could be used for validating, if used for matching it matches the text between two slashes with a %20
including the leading and trailing slash.
If you are using a regex implemenation that supports arbitrarily long lookaround constructs (like .NET) you could use
(?<=.*/)%20(?=.*/)
I tried this one in .Net and it returned the three first links. I am not much of a regex guy, so this can most likely be done much more elegant way than my example.
(\w*\.*)*/(\w*)*%20(\w*)*/(\w*\.*)*
@Lekensteyn was right but skipped escaping forward slashes in the character class [^/]*
.
Instead of:
/\/[^/]*%20[^/]*\//
use:
/\/[^\/]*%20[^\/]*\//
精彩评论