How to non-greedy multiple lookbehind matches

后端 未结 3 1729
南方客
南方客 2020-12-17 06:49
Source:    
Engine:    PCRE

RegEx1:    (?<=)(.*)(?=

        
相关标签:
3条回答
  • 2020-12-17 07:25

    Put something greedy in front of it?

    (?:.*)(?<=<prefix>)(.*)(?=<suffix2>)
    

    Since the greedy (?:.*) will gobble as much as it can, only the minimum will be matched by the rest of the pattern - effectively making the rest non-greedy.

    The non-greedy .*? might also work:

    (?<=<prefix>)(.*?)(?=<suffix2>)
    
    0 讨论(0)
  • 2020-12-17 07:34

    I just had the same problem. But in my case it was

    (?<=<prefix>)(?:.(?!<prefix>))*(?=<suffix>)
    

    That did what I wanted.

    This expression will match anything that is a concatenation of characters between <prefix> and <suffix> and doesn't contain the substring <prefix>. (I think so. I'm not very good at regexp.)

    0 讨论(0)
  • 2020-12-17 07:43

    I suggest you use:

    (?<=<prefix>)(((?!<prefix>).)*)(?=<suffix2>)
    

    This makes sure that there can be no <prefix> inside the match. The complete match result will be <content2>

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