So I have a string like this:
test //ita https://example.com lics// test // another // one
I can capture text between 2 "//" strings easy enough like so:
//(.*?)//
Which will return the groups ita https:
and test
however I’m trying to get it to ignore the cases where there is a "http://" or "https://".
So I’m trying to get it so that it only returns ita https://example.com lics
and another
.
3
Answers
Looks like your on the right track.
Heres how it works
You should post a few string examples to better test but based on what I tried, looks like this works.
You can use this regex to match your strings:
This will match:
(?<!https:|http:)//
://
, not preceded byhttps:
orhttp:
s*
: some amount of whitespace((?:https?://|(?!//).)+)
: capture group 1, some number of either:https?://
://
preceded byhttps:
orhttp:
; or(?!//).
: a character which is not the start of//
(?<!s)s*
: some amount of whitespace, not preceded by whitespace (this prevents capturing any whitespace before the closing//
in group 1)//
: literal//
Regex demo on regex101
The strings you are interested in will be captured in group 1. In PHP:
Output:
PHP demo on 3v4l.org
I propose this solution with control verbs
(*SKIP)
and(*F)
.Here’s the regex101 proof and PHP proof