• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lumuy
V2EX  ›  程序员

包含子字符串 A, B 且 A B 至少出现一次的正则写法

  •  
  •   Lumuy · Oct 8, 2016 · 3558 views
    This topic created in 3566 days ago, the information mentioned may be changed or developed.
    现有子个字符串 A 和 B
    我要匹配字符串 C
    满足条件 C 必须包含子字符串 A B ,顺序不论

    求正则写法
    11 replies    2016-10-09 00:12:14 +08:00
    iEverX
        1
    iEverX  
       Oct 8, 2016
    暴力点, A.*B|B.*A
    xcv58
        2
    xcv58  
       Oct 8, 2016
    这样行不行?
    (.*A?.*B?.*)|(.*B?.*A?.*)
    crayygy
        3
    crayygy  
       Oct 8, 2016
    不太熟悉正则,我之前的方法是对 A 和 B 分别判断,然后 &&,可能有点蠢了。
    xcv58
        4
    xcv58  
       Oct 8, 2016
    @xcv58 傻逼了,看 1L 的方法吧。
    Lumuy
        5
    Lumuy  
    OP
       Oct 8, 2016
    @iEverX
    @xcv58

    不太好啊,我还有隐藏的 C D...呢
    sutra
        6
    sutra  
       Oct 8, 2016 via iPhone
    .*(?:A.*B)|(?:B.*A).*
    asj
        7
    asj  
       Oct 8, 2016
    (?=.*A.*)(?=.*B.*).*
    66beta
        8
    66beta  
       Oct 8, 2016
    tees
        9
    tees  
       Oct 8, 2016
    写个比较明确的需求吧。
    A 和 B 是单个字母还是个单词,还是单词和数字的组合。
    sutra
        10
    sutra  
       Oct 8, 2016
    @tees 我觉得他说的是变量名。哈。
    msg7086
        11
    msg7086  
       Oct 9, 2016
    @Lumuy 内容比较多的话为什么不考虑开个循环呢?
    如果是字符串的话直接检查存在性就行了,比正则快些。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5402 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 08:19 · PVG 16:19 · LAX 01:19 · JFK 04:19
    ♥ Do have faith in what you're doing.