Хорошо, ничто не может побить конструкцию mickmackusa \K
.
Но для двигателей с дефектами \ K это следующая лучшая вещь
(\d(?<=ds_user_id=\d)\d*)(?=;)
Объяснено
( # (1 start), Consume many ID digits
\d # First digit of ID
(?<= ds_user_id= \d ) # Look behind, assert ID key exists before digit
\d* # Optional the rest of the digits
) # (1 end)
(?= ; ) # Look ahead, assert a colon exists
Это решение глагола (без \ K), примерно на 30% быстрее.
( # (1 start), Consume many ID digits
\d # First digit of ID
(?:
(?<! ds_user_id= \d ) # Look behind, if not ID,
\d* # get rest of digits
(*SKIP) # Fail, then start after this
(?!)
|
\d* # Rest of ID digits
)
) # (1 end)
(?= ; ) # Look ahead, assert a colon exists
Некоторые тесты для сравнения
Regex1: (\d(?:(?<!ds_user_id=\d)\d*(*SKIP)(?!)|\d*))(?=;)
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 1
Elapsed Time: 0.53 s, 534.47 ms, 534473 µs
Matches per sec: 93,550
Regex2: (\d(?<=ds_user_id=\d)\d*)(?=;)
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 1
Elapsed Time: 0.80 s, 796.97 ms, 796971 µs
Matches per sec: 62,737
Regex3: ds_user_id=\K\d+(?=;)
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 1
Elapsed Time: 0.21 s, 214.55 ms, 214549 µs
Matches per sec: 233,046
Regex4: ds_user_id=(\d+)(?=;)
Options: < none >
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 1
Elapsed Time: 0.23 s, 231.23 ms, 231233 µs
Matches per sec: 216,232