Examples of using "reluctant" subexpressions in regexps?

Discussion in 'Java' started by david.karr@wamu.net, Apr 26, 2005.

  1. Guest

    I'm preparing a presentation about regular expressions to a group of my
    colleagues. I'm looking for examples of expressions using the
    "reluctant" quantifier (the "?"). I'm not asking about the syntax,
    just real (or simulated) examples of why you would want to use this.
    Note that I'm not referring to using this on "simple" expressions, like
    "a?", but on already quantified expressions, like "a+?" or "a*?", or
    even "a{n}?". I wouldn't even mention the latter one, as it doesn't
    seem to make sense to me, but it's used as an example (with no real
    explanation) in the "perlre" documentation page.
    , Apr 26, 2005
    #1
    1. Advertising

  2. writes:

    > I'm preparing a presentation about regular expressions to a group of my
    > colleagues. I'm looking for examples of expressions using the
    > "reluctant" quantifier (the "?"). I'm not asking about the syntax,
    > just real (or simulated) examples of why you would want to use this.


    First of all, I don't believe there is any regular expression that can
    be written with a non-greedy match, that can't also be written using
    only greedy matches (although perhaps with look-ahead).

    One example is matching all HTML tags (e.g., for removal):
    /<.*?>/g

    Generally, non-greedy matching is useful when you want to match a
    delimited substring several times inside a string. With greedy match,
    you match from the first start marker to the last end marker, and you
    have to write your regexp to avoid matching more than one end marker.
    With a non-greedy match, as above, you just match from start to the
    first end.

    Good luck
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 26, 2005
    #2
    1. Advertising

  3. HK Guest

    wrote:
    > I'm preparing a presentation about regular expressions to a group of

    my
    > colleagues. I'm looking for examples of expressions using the
    > "reluctant" quantifier (the "?").


    Under certain circumstances it is nice to match
    a whole XML element, even with nested elements,
    as long as it does not contain itself.

    There are circumstances where this, however,
    does not work. The example given on

    http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/jfa/doc-files/resyntax.html#greed

    under "Non Greedy Matching vs. Shortest Match"
    may clarify it a bit. The "shortest match"
    operator is, however, not available in
    java.util.regex.

    On said page is a nasty typo: it should read:

    "just because the longer match satisfies
    the regular expression, while stopping
    at the first </tag> would **not** match."

    You'll note where the 'not' is missing.-)

    Harald.
    HK, Apr 27, 2005
    #3
  4. Alan Moore Guest

    On 26 Apr 2005 11:33:12 -0700, wrote:

    >I'm preparing a presentation about regular expressions to a group of my
    >colleagues. I'm looking for examples of expressions using the
    >"reluctant" quantifier (the "?"). I'm not asking about the syntax,
    >just real (or simulated) examples of why you would want to use this.
    >Note that I'm not referring to using this on "simple" expressions, like
    >"a?", but on already quantified expressions, like "a+?" or "a*?", or
    >even "a{n}?". I wouldn't even mention the latter one, as it doesn't
    >seem to make sense to me, but it's used as an example (with no real
    >explanation) in the "perlre" documentation page.


    Heh! I'm surprised they put that in there with no disclaimer. True,
    it's correct *syntax*, but the question mark has no effect. In the
    javadoc for java.util.regex.Pattern, they do the same thing with the
    possessive '+':

    X{n}+ X, exactly n times

    ....which is just as pointless as X{n}?.
    Alan Moore, Apr 27, 2005
    #4
  5. . Guest

    On Tue, 26 Apr 2005 wrote:

    > I'm preparing a presentation about regular expressions to a group of my
    > colleagues. I'm looking for examples of expressions using the
    > "reluctant" quantifier (the "?"). I'm not asking about the syntax,
    > just real (or simulated) examples of why you would want to use this.
    > Note that I'm not referring to using this on "simple" expressions, like
    > "a?", but on already quantified expressions, like "a+?" or "a*?", or
    > even "a{n}?". I wouldn't even mention the latter one, as it doesn't
    > seem to make sense to me, but it's used as an example (with no real
    > explanation) in the "perlre" documentation page.


    The best example I have seen is:

    This is <bold>an example</bold> of HTML.

    If we were looking for the tag <bold> the regular expression <.*> will
    match <bold>an example</bold>. Too long because .* is greedy. By changing
    the regular expression to <.*?> we get the desired results.

    This is actually the only place I've seen the reluctant quantifier used.

    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
    ., Apr 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    513
  2. Daniel Bickett

    Help! Host is reluctant to install Python

    Daniel Bickett, Jan 25, 2005, in forum: Python
    Replies:
    7
    Views:
    322
  3. Steve Pope
    Replies:
    43
    Views:
    1,010
    Victor Bazarov
    Oct 15, 2010
  4. James Kanze
    Replies:
    4
    Views:
    310
    Niklas Holsti
    Oct 14, 2010
  5. Replies:
    1
    Views:
    113
    Sherm Pendley
    Mar 20, 2005
Loading...

Share This Page