Expression Quantifiers: Greedy, Reluctant, and Possessive Behavior

By default, quantifiers are greedy. Greedy means that the expression accepts as many tokens as possible, while still permitting a successful match. You can override this behavior by appending a '?' for reluctant matching or '+' for possessive matching.

Reluctant matching means that the expression accepts as few tokens as possible, while still permitting a successful match.

Possessive matching means that the expression accepts as many tokens as possible, even if doing so prevents a match.

  • One or More Quantifier Example

    Greedy

    1. The Greedy behavior in <Field1> accepts the maximum number of tokens that match the rule, while giving up tokens only when necessary to match the remaining rules.
    2. <Field2> can only accept the minimum number tokens that <Field1> is forced to give up.
    3. <Field3> can only accept a single token that <Field1> is forced to give up.

    Reluctant

    1. The reluctant behavior in <Field1> accepts the minimum number of tokens that match the rule while giving up tokens only when necessary to match the remaining rules.
    2. Because <Field2> is greedy, it accepts the maximum number of tokens given up by <Field1>, while giving up tokens only when necessary to match the remaining rules.
    3. <Field3> can only accept a single token that <Field2> is forced to give up.

    Possessive

    1. The possessive behavior in <Field1> accepts the maximum number of tokens that match the rule, while not giving up any tokens to match the remaining rules.
    2. Because <Field1> is possessive, there are no tokens available for <Field2>. 3. Because <Field1> is possessive, there are no tokens available for <Field3>.
    3. The input is not parsed.

    Zero or More Quantifier Example

    Greedy

    1. The Greedy behavior in <Field1> accepts no tokens or the maximum number of tokens that match the rule, while giving up tokens only when necessary to match the remaining rules.
    2. Because <Field1> is greedy, <Field2> only accepts the minimum number tokens that <Field1> is forced to give up. Since the minimum for <Field2> is zero, zero tokens match this rule.
    3. Because <Field1> is greedy, <Field3> only accepts a single token that <Field1> rule is forced to give up.

    Reluctant

    1. The reluctant behavior in <Field1> accepts no tokens or the minimum number of tokens that match the rule while giving up tokens only when necessary to match the remaining rules.
    2. Because <Field2> is greedy, it accepts the maximum number of tokens given up by <Field1>, while giving up tokens only when necessary to match the remaining rules.
    3. <Field3> can only accept a single token that <Field2> is forced to give up.

    Possessive

    1. 1. The possessive behavior in <Field1> accepts no tokens or the maximum number of tokens that match the rule while not giving up any tokens to match the remaining rules.
    2. Because <Field1> is possessive, there are no tokens available for <Field2>.
    3. Because <Field1> is possessive, there are no tokens available for <Field3>. The input is not parsed.

    Zero or One Quantifier Example

    Greedy

    1. The Greedy behavior in <Field1> accepts no tokens or the maximum number of tokens that match the rule, while giving up tokens only when necessary to match the remaining rules.
    2. <Field2> can only accept the minimum number tokens that <Field1> is forced to give up.
    3. <Field3> can only accept a single token that <Field1> is forced to give up.

    Reluctant

    1. The reluctant behavior in <Field1> accepts the minimum number of tokens that match the rule while giving up tokens only when necessary to match the remaining rules.
    2. Because <Field2> is greedy, it accepts the maximum number of tokens given up by <Field1>, while giving up tokens only when necessary to match the remaining rules.
    3. <Field3> can only accept a single token that <Field2> is forced to give up.

    Possessive

    1. The possessive behavior in <Field1> accepts no tokens or the maximum number of tokens that match the rule, while not giving up any tokens to match the remaining rules.
    2. Because <Field1> is possessive, there is only one token available for <Field2>.
    3. Because <Field1> is possessive, there are no tokens available for <Field3>. The input is not parsed.

  • Min/Max Quantifier Example

    Greedy

    1. The Greedy behavior in the <Field1> rule accepts the maximum number of tokens that match the rule, while giving up tokens only when necessary to match the remaining rules.
    2. <Field2> can only accept the minimum number tokens that <Field1> is forced to give up.
    3. <Field3> can only accept a single token that <Field1> is forced to give up.

    Reluctant

    1. The reluctant behavior in <Field1> accepts the minimum number of tokens that match the rule while giving up tokens only when necessary to match the remaining rules.
    2. Because <Field2> is greedy, it accepts the maximum number of tokens given up by <Field1>, while giving up tokens only when necessary to match the remaining rules.
    3. <Field3> can only accept a single token that <Field2> is forced to give up.

    Possessive

    1. The possessive behavior in <Field1> accepts the maximum number of tokens that match the rule, while not giving up any tokens to match the remaining rules.
    2. Because <Field1> is possessive, there are two tokens available for <Field2>.
    3. <Field3> can only accept a single token that <Field2> is forced to give up.