This discussion is archived
7 Replies Latest reply: Aug 29, 2007 8:51 PM by 800351 RSS

writing recursive regular expression in java

807605 Newbie
Currently Being Moderated
how can i write a recursive regular expression statement in java?
can anyone help me on that. for instance if i have the defination

statement-list := statement | statement-list;

how can i express this in java?
  • 1. Re: writing recursive regular expression in java
    807605 Newbie
    Currently Being Moderated
    this have to do with your other post? because if so i posted a solution that should work
  • 2. Re: writing recursive regular expression in java
    807605 Newbie
    Currently Being Moderated
    http://forum.java.sun.com/thread.jspa?threadID=5202390&tstart=0
  • 3. Re: writing recursive regular expression in java
    807605 Newbie
    Currently Being Moderated
    i didn't mean the question before to this. it's like if i have to write a regular expression as such

    expression = expressionlist | expression which is a recursive statement. how do i do this in java? bcoz every regular expression i
    treat as string so how can i produce recursion in string in such a way?
  • 4. Re: writing recursive regular expression in java
    807605 Newbie
    Currently Being Moderated
    In the simplest case, you just add a plus sign:
    (statememt)+
    But it's almost never that simple.
  • 5. Re: writing recursive regular expression in java
    EJP Guru
    Currently Being Moderated
    a recursive regular expression
    This is a contradiction in terms.

    Regular expressions cannot express recursion. They can only express iteration as per the previous post. For true recursion you need a DPDA.
  • 6. New question..
    807605 Newbie
    Currently Being Moderated
    Hello..

    I wanted to write a regular expression to match the foll string..

    <!--endclickprintexclude--><!--startclickprintexclude--> <!--endclickprintexclude-->
    <p> <b>NEW ORLEANS, Louisiana (CNN) </b>

    -- Two years after Hurricane Katrina devastated coastal areas of Louisiana and Mississippi, residents say much of America has forgotten their plight.

    </p> <!--startclickprintexclude-->

    I tried doing..
    Matcher matcher= Pattern.compile("<!--endclickprintexclude--> <p><b>([^<^>]+?)</p><!--startclickprintexclude-->", Pattern.CASE_INSENSITIVE).matcher(story);

    Its not working...
    is there any other soln?
  • 7. Re: New question..
    800351 Newbie
    Currently Being Moderated
    What do you want to match and get? Describe your requirement in plain English. Don't use cryptic ?nglish words, please.

    Here's a stab in the dark. It should have nothing to do with recursion.
    import java.util.regex.*;
    
    public class Bkb{
    
      public static void main(String[] args){
        String story = "<!--endclickprintexclude--><!--startclickprintexclude--> <!--endclickprintexclude-->\n<p> <b>NEW ORLEANS, Louisiana (CNN) </b>\n\n-- Two years after Hurricane Katrina devastated coastal areas of Louisiana and Mississippi, residents say much of America has forgotten their plight.\n\n</p> <!--startclickprintexclude-->";
        String regex = "<!--endclickprintexclude-->\\s*<p>\\s*<b>(.+?)</b>(.+)</p>\\s*<!--startclickprintexclude-->";
    
        Pattern pat 
          = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
        Matcher mat = pat.matcher(story);
    
        while (mat.find()){
          System.out.print(mat.group(1));
          System.out.print(mat.group(2));
        }
      }
    }
    Message was edited by:
    hiwa