Restricting Positive Lookahead when capturing

I get some text at intervals from stream like,

ICY Info: StreamTitle='Elvis Presley - Saved';StreamUrl='';

ICY Info: StreamTitle='Elvis Presley - Saved'

ICY Info: StreamTitle='Ivank'av T'ali - Yorua';StreamUrl='';

ICY Info: StreamTitle='Ivank'av T'ali - Yorua'

I wish to obtain

Elvis Presley - Saved
Elvis Presley - Saved
Ivank'av T'ali - Yorua
Ivank'av T'ali - Yorua

I'm using (?<=\=\').*(?=';S) or (?<=\=\').*(?=') but they seem not suitable.

Demo

@Edit: I have just come with (?<=\=\').*?(?=';).

Upvotes: 0

Views: 39

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

(?=';S) will not work for the first and the third example when ';S is not there.

On the other hand (?=') Will match too much in the first and the third example.

What you could do is use an alternation in the lookahead to check for either ;: or ' followed by the end of the string.

(?<==').+?(?='(?:;|$))

Regex demo

Explanation

  • (?<==') Positive lookbehind to assert what is on the left is ='
  • .+? Match any character one or more times non greedy
  • (?= Positive lookahead to assert that what is on the right is
    • '(?:;|$) Match ' followed by an alternation matching either ; or assert the end of the string $
  • ) close positive lookahead

Upvotes: 1

Related Questions