Reputation: 10659
I'm trying to create a regexp that can find occurrences of /
from a string however the following rules must be satisfied:
/
is a separator between each string, e.g: /string1/string2/string3//
is also a separator between regular expressions like /regexp1//regexp2//regexp3/The goal is to find all occurrences of the separator /
that satisfy such a condition
As a result, I would like to get the separators between the following phrases
string1
string2
string3
/regexp1/
/regexp2/
/regexp3/
string4
/string1/string2/string3//regexp1///regexp2///regexp3//string4/
Currently I prepared the following regexp, but unfortunately it doesn't work as I expect, because it doesn't handle when there are 2 regexps next to each other. Does anyone have any advice how to overcome such case?
((?<=\/)\/(?=\/)|(?<!\/)\/(?!\/)|(?<=\w)\/(?=\/)|(?<=\/)\/(?=\w)|\/$)
Upvotes: 1
Views: 355
Reputation: 786081
You may use this regex with an alternation and grab capture group #1:
(?<=\/)(\/[^\/]+\/|[^\/]+)(?:\/|$)
RegEx Details:
(?<=\/)
: Assert that previous character is /
(
Start capture group #1
\/
: Match a /
: Match 1+ non-
/` characters\/
: Match a /
|
: OR[^\/]+
: Match 1+ non-/
characters)
: End capture group #1(?:\/|$)
: Match a /
or end positionUpvotes: 1