0

Need some help with the regex to be used for extracting string between a start_pattern and an end_pattern. Additionally, the regex should grep all characters upto the end of line if no end_pattern exists.

Sample 1 : "BOOK1:book1A,book1B,book1C,book1D" 

Expected Result : book1A,book1B,book1C,book1D

Sample 2 : "BOOK1:book1A,book1B,book1C,book1D|BOOK2:book2A,book2B,book2C,book2DA"

Expected Result : (1)book1A,book1B,book1C,book1D (2)book2A,book2B,book2C,book2DA

I've managed to resolve the regex (shown below) when the string terminator is "|", but cannot get around to resolving it when there is no terminator

(?<=BOOK1:).*(?=\|)
4

1 回答 1

2

使用$并更改.*.*?

(?<=BOOK1:|\|).*?(?=\||$)

$标记行或字符串的结尾

.*?会懒惰地匹配


例如,对于输入

a|b|c|d|e

用正则表达式

(?<=\|).*(?=\|)

它会匹配b|c|d

用正则表达式

(?<=\|).*?(?=\|)

它会匹配

b
c
d
于 2013-07-04T14:28:59.517 回答