为了账号安全,请及时绑定邮箱和手机立即绑定

如何从字符串中提取 ID 号?

如何从字符串中提取 ID 号?

PHP
守着一只汪 2021-06-30 14:57:39
如何使用正则表达式或 preg_match 检索中间值?$str = 'fxs_124024574287414=base_domain=.example.com; datr=KWHazxXEIkldzBaVq_of--syv5; csrftoken=szcwad; ds_user_id=219132; mid=XN4bpAAEAAHOyBRR4V17xfbaosyN; sessionid=14811313756%12fasda%3A27; rur=VLL;'我如何只从ds_user_id使用正则表达式或 中获取值preg_match?
查看完整描述

3 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

好吧,没有什么能打败 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


查看完整回答
反对 回复 2021-07-09
  • 3 回答
  • 0 关注
  • 252 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信