简体   繁体   中英

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 (?<=\\=\\').*?(?=';) .

(?=';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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM