gpt4 book ai didi

regex - 使用正则表达式从序列中拆分字符串

转载 作者:行者123 更新时间:2023-11-29 09:10:23 24 4
gpt4 key购买 nike

我需要一个匹配字母和数字但不匹配序列“00”的正则表达式。

例如“hello00world00number001”应匹配:“hello”、“world”、“number”和“1”。

我测试没有成功:

(?:[\w](?<!00))+

编辑:“hello000world0000number000001”必须分成:“hello0”“world”“number0”和“1”

最佳答案

输入字符串:hello000world0000number00000100test00test20

拆分

  1. 如果遇到类似 0000 的系列,单独按 00 拆分将生成空匹配:
    输出:hello/0world//number//01/test/test20

  2. 为了解决这个问题,让我们在一个组中包含 2 个零:
    正则表达式:(00)+ - 系列中的最后一个不均匀 0 进入下一场比赛 - live demo
    输出:hello/0world/number/01/test/test20

  3. 使用否定前瞻:
    正则表达式:(00)+(?!0) - 在第一场比赛中保留不均匀系列中的第一个 0 - live demo
    输出:hello0/world/number0/1/test/test20

匹配

  1. 只有 00 的结果不正确
  2. /([a-z0-9]+?)(?:(?:00)+|$)/gi - live demo
  3. /([a-z0-9]+?)(?:(?:00)+(?!0)|$)/gi - live demo

关于regex - 使用正则表达式从序列中拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16427283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com