RegEx匹配PHP

Data:

N15319542045C13_1_3/61488007C13-130083_1_3/61488007C13-130083-1_1_3/P1197443641_1_3SD|1
NP1196939393_1_3SU|OD=2/7;|BNP1196939393_1_3SU|OD=2/7;|BNP1196930222_1_3SU|OD=4/11;|
NP1196930222_1_3SU|OD=4/11;|
N15319384625C13_1_3/61445794C13-130077_1_3SD||BN15319384625C13_1_3/61445794C13-130077_1_3SD||

RegExp:

(N(.*?)S([UID])\|(.*?))(?:B|\|.?$)

I am trying to find 7 matches using above regex but only 6 are matching. Not sure how to fix to match 1st line as well.

Format:

N(key)S(action)|(value or end)

end depend on different matches

I solved it if someone else needs:

(\x15(.*?)\x01([UID])\|(.*?))(?:.*?\x08|.*\|?$)

The regex didn't work because after the S[UID] you expect 2 | as per the regex but in the first input string there is only one.

One fix is to make the second group optional and move out the string end anchor $

(N(.*?)S([UID])\|(.*?))(?:B|\|.?)?$

Regex Demo


Or may be more simpler as
N.*?S[UID]\|.*$

Regex Demo