Regex Puzzle

Discussion in 'Java' started by Roedy Green, Dec 14, 2008.

  1. Roedy Green

    Roedy Green Guest

    Why won't this regex match this string?


    <blockquote>&ldquo;The secret is surrender. Commitment to Christ
    involves surrender of the
    intellect, the emotions and the will &mdash; the total
    person.&rdquo;
    <br>
    ~ <span class="christian">Bill Bright</span>, <cite>Jesus and the
    Intellectual</cite></blockquote>



    private static final Pattern CHRISTIAN = Pattern.compile(
    "<blockquote>[.]+<span
    class=\"christian\">[.]+</span>[.]+</blockquote>", Pattern.DOTALL );

    I have stared at it for 30 minutes and tried many variations to no
    avail.

    Any tips on debugging problems of this sort?

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
    However, he refuses to consider the costs of not implementing Kyoto which the
    famous economist Nicholas Stern estimated at 5 to 20% of GDP
     
    Roedy Green, Dec 14, 2008
    #1
    1. Advertising

  2. Roedy Green

    Roedy Green Guest

    On Sat, 13 Dec 2008 22:37:21 -0800, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >private static final Pattern CHRISTIAN = Pattern.compile(
    >"<blockquote>[.]+<span
    >class=\"christian\">[.]+</span>[.]+</blockquote>", Pattern.DOTALL );


    use plain . not [.] [.] means literal period, not any-char.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
    However, he refuses to consider the costs of not implementing Kyoto which the
    famous economist Nicholas Stern estimated at 5 to 20% of GDP
     
    Roedy Green, Dec 14, 2008
    #2
    1. Advertising

  3. On 14.12.2008 07:37, Roedy Green wrote:
    > Why won't this regex match this string?


    > I have stared at it for 30 minutes and tried many variations to no
    > avail.


    As I see you finally figured yourself.

    > Any tips on debugging problems of this sort?


    Cut down the regexp, start building it piece by piece and match it
    against your text sequence until it stops matching. The last addition
    is the "culprit".

    Cheers

    robert
     
    Robert Klemme, Dec 14, 2008
    #3
  4. Roedy Green wrote:
    > Why won't this regex match this string?
    >
    >
    > <blockquote>&ldquo;The secret is surrender. Commitment to Christ
    > involves surrender of the
    > intellect, the emotions and the will &mdash; the total
    > person.&rdquo;
    > <br>
    > ~ <span class="christian">Bill Bright</span>, <cite>Jesus and the
    > Intellectual</cite></blockquote>
    >
    >
    >
    > private static final Pattern CHRISTIAN = Pattern.compile(
    > "<blockquote>[.]+<span
    > class=\"christian\">[.]+</span>[.]+</blockquote>", Pattern.DOTALL );
    >
    > I have stared at it for 30 minutes and tried many variations to no
    > avail.
    >
    > Any tips on debugging problems of this sort?


    Try the reluctant quantifier[1,2] `X*?' like so:

    Pattern p = Pattern.compile("<blockquote>.*?<span\\s+class=\"christian\">.*?<\\/span>.*?<\\/blockquote>", Pattern.DOTALL);



    [1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
    [2] http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
     
    Kim A. Brandt, Dec 14, 2008
    #4
  5. Roedy Green

    Jan Thomä Guest

    On 2008-12-14 07:37:21 +0100, Roedy Green
    <> said:

    > Any tips on debugging problems of this sort?


    If you are using IntellIJ there is a nice regex plugin which helps a
    whole lot in creating complex regular expressions.

    Jan
     
    Jan Thomä, Dec 15, 2008
    #5
  6. Roedy Green

    GK Guest

    On Dec 15, 12:40 pm, Jan Thomä <> wrote:

    > If you are using IntellIJ there is a nice regex plugin which helps a
    > whole lot in creating complex regular expressions.


    If you are using Eclipse try this one; it's called QuickRex.
    Eclipse-Update-Site: http://www.bastian-bergerhoff.com/eclipse/features

    Gernot
     
    GK, Dec 16, 2008
    #6
    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. G. Stewart

    regex puzzle!

    G. Stewart, Nov 23, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    529
    G. Stewart
    Nov 25, 2004
  2. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    736
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  3. Jeremy Bowers

    a RegEx puzzle

    Jeremy Bowers, Mar 11, 2005, in forum: Python
    Replies:
    4
    Views:
    374
    Kent Johnson
    Mar 11, 2005
  4. Replies:
    3
    Views:
    809
    Reedick, Andrew
    Jul 1, 2008
  5. Roedy Green

    regex puzzle

    Roedy Green, Apr 9, 2014, in forum: Java
    Replies:
    8
    Views:
    139
    Arne Vajhøj
    Apr 12, 2014
Loading...

Share This Page