Pattern in lookbehind

后端 未结 3 1381
予麋鹿
予麋鹿 2021-01-22 07:22

My question is related with lookbehinds, I want to find all the first numbers after the word \"this\", I have the following data:

188282 this is an exam

相关标签:
3条回答
  • 2021-01-22 08:00

    Yes, you can use patterns inside lookbehinds, but that you can't do in most flavor of regex is to have a variable length lookbehind. In other words, you can't use a quantifier (but a fixed quantifier like {n} is allowed) inside a lookbehind. But some regex flavour allows you to use the alternation | or a limited (like in java) quantifier {1,n}.

    With .net languages variable length lookbehinds are allowed.

    0 讨论(0)
  • 2021-01-22 08:07

    The thing with lookbehinds is that not all languages support variable width lookbehinds (they can't support lookbehinds where what's inside can be of variable number of characters).

    What you can do, might be using a lookahead and a capture group:

    (?=this[^\d]+(\d+))
    

    regex101 demo

    Or maybe the \K regex character which resets a match (if your regex engine supports it).

    this[^\d]+\K\d+
    

    regex101 demo

    0 讨论(0)
  • 2021-01-22 08:17

    It depends on your implementation of regex. You'll have to do some testing for sure.

    I know that some implementations don't like this:

    (?<=\d{1,5}) or (?<=\w*)

    But they will work fine with this:

    (?<=\d{5}) or (?<=\w{1000})

    In other words, no repetition or flexible lengths.

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