How to convert regexp from lookahead

前端 未结 1 522
情书的邮戳
情书的邮戳 2021-01-23 16:44

I have the following Regex in PHP and other code that is doing a great job.

/^(?:(?=[^ ]+\\d)(?:[A-Z0-9]+))|(?:[A-Z0-9]+) +?(?=.*\\d)(?:[A-Z0-9]+)?
相关标签:
1条回答
  • 2021-01-23 17:26

    Firstly, the left hand side of the alternation /^... matches nothing, because / can never appear before start of input.

    That leaves just (?:[A-Z0-9]+) +?(?=.*\d), which can be expressed as:

    ((?:[A-Z0-9]+) ).*\d.*
    

    The term " +?" as used is identical in effect to just " ".

    Note however that this consumes more input than the original, which is unavoidable. Your original match is now in group 1.

    0 讨论(0)
提交回复
热议问题