How to match, but not capture, part of a regex?

前端 未结 7 1537
清酒与你
清酒与你 2020-11-28 00:10

I have a list of strings. Some of them are of the form 123-...456. The variable portion \"...\" may be:

  • the string \"apple\" followed by a hyphen,
相关标签:
7条回答
  • 2020-11-28 00:56

    I have modified one of the answers (by @op1ekun):

    123-(apple(?=-)|banana(?=-)|(?!-))-?456
    

    The reason is that the answer from @op1ekun also matches "123-apple456", without the hyphen after apple.

    0 讨论(0)
  • 2020-11-28 00:57

    The only way not to capture something is using look-around assertions:

    (?<=123-)((apple|banana)(?=-456)|(?=456))
    

    Because even with non-capturing groups (?:…) the whole regular expression captures their matched contents. But this regular expression matches only apple or banana if it’s preceded by 123- and followed by -456, or it matches the empty string if it’s preceded by 123- and followed by 456.

    |Lookaround  |    Name      |        What it Does                       |
    -----------------------------------------------------------------------
    |(?=foo)     |   Lookahead  | Asserts that what immediately FOLLOWS the |
    |            |              |  current position in the string is foo    |
    -------------------------------------------------------------------------
    |(?<=foo)    |   Lookbehind | Asserts that what immediately PRECEDES the|
    |            |              |  current position in the string is foo    |
    -------------------------------------------------------------------------
    |(?!foo)     |   Negative   | Asserts that what immediately FOLLOWS the |
    |            |   Lookahead  |  current position in the string is NOT foo|
    -------------------------------------------------------------------------
    |(?<!foo)    |   Negative   | Asserts that what immediately PRECEDES the|
    |            |   Lookbehind |  current position in the string is NOT foo|
    -------------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-11-28 00:59

    A variation of the expression by @Gumbo that makes use of \K for resetting match positions to prevent the inclusion of number blocks in the match. Usable in PCRE regex flavours.

    123-\K(?:(?:apple|banana)(?=-456)|456\K)
    

    Matches:

    Match 1  apple
    Match 2  banana
    Match 3
    
    0 讨论(0)
  • 2020-11-28 01:00

    By far the simplest (works for python) is '123-(apple|banana)-?456'.

    0 讨论(0)
  • 2020-11-28 01:05

    Try this:

    /\d{3}-(?:(apple|banana)-)?\d{3}/
    
    0 讨论(0)
  • 2020-11-28 01:08

    Try:

    123-(?:(apple|banana|)-|)456
    

    That will match apple, banana, or a blank string, and following it there will be a 0 or 1 hyphens. I was wrong about not having a need for a capturing group. Silly me.

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