Home arrow static arrow Java Programming [Archive] - Regex recursion explain
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Regex recursion explain
This topic has 57 replies on 4 pages.    1 | 2 | 3 | 4 | Next »

Posts:357
Registered: 8/5/04
Regex recursion explain   
Aug 7, 2004 4:21 PM



 

Hi People!

Please explain me this code with few more comments and also
please ans. the comments in the code.
::grin::
Thank you!
If you don't understand the question just type ::WHAT::? thanks!

public class Regex {  private String pattern;   public Regex(String s) {    pattern = s;  }   public boolean matches(String v) {    return domatch(pattern, v);  }   private boolean domatch(String p, String v) {    System.out.println("domatch |"+p+"|-|"+v+"|");    if(p.length() == 0)       return v.length() == 0; //check length of other string     // p.length() > 0    char c = p.charAt(0); //First character    switch(c){    case '*': // 0 or more characters      if(domatch(p.substring(1), v)) //excluding 1 char if domatch        return true; //why true wouldn't it be FALSE since its excluding        //1st character?  by p.substring(1) i.e 1 and NOT 0?       if(v.length() == 0)         return false;      return domatch(p, v.substring(1));    case '?': // 1 character      if(v.length() == 0)        return false;      return domatch(p.substring(1), v.substring(1)); //OK    default:      if(v.length() == 0)        return false;      char x = v.charAt(0);       if(c == x)        return domatch(p.substring(1), v.substring(1));      return false;    }  }      public static void main(String[] args) {    if(args.length != 2) {      System.out.println("Usage: Regex pattern value");      System.exit(0);    }    Regex r = new Regex(args[0]);    System.out.println("Match " + r.matches(args[1]));  }}
 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 4:33 PM (reply 1 of 57)



 
You didn't write that. You don't care what it means. You didn't ask a question.

I challenge you to ask your question clearly, and not just repost someone else's code.

/k1
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 4:38 PM (reply 2 of 57)



 
OK -

What is '*" and '?' for ?

and i don't understand why is the code comparing from

p.substring(1) instead of p.substring(0)
 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 4:46 PM (reply 3 of 57)



 
OK -

Let's take this step by step.

First you appear to admit that my first assumption was correct -

you did not write that code?

Correct?

/k1
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 4:50 PM (reply 4 of 57)



 
LOL...

If I had written that code then why wouldn't I understand it Mr. Komone?

Anyways, now can you please ans. the question/s?
 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 4:57 PM (reply 5 of 57)



 
You didn't answer mine directly.

So let's assume your answer is NO I DID NOT WRITE THIS CODE.

OK - at first sight that code attempts to do at the command line certain functionality for which java.util.regex already provides more than adequate functionality (of course it doesn't really but that's what it purports to be from the naming used).

So my second question is : why do you care how this code snippet works?

/k1
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 4:58 PM (reply 6 of 57)



 
because its given on the course site and it might be asked in TEST
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 5:01 PM (reply 7 of 57)



 

OK - at first sight that code attempts to do at the
command line certain functionality for which
java.util.regex already provides more than adequate
functionality (of course it doesn't really but that's
what it purports to be from the naming used).

So my second question is : why do you care how this
code snippet works?
Now I am lost
 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 5:03 PM (reply 8 of 57)



 
OK -
So is this your homework or mine?
/k1
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 5:08 PM (reply 9 of 57)



 
LOL...

this is NOT homework mr. komone

this is a sample code i, i am practising with; to prepare myself for similar type of question on Test
 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 5:09 PM (reply 10 of 57)



 
OK -
Now ask yourself - "is the question I'm asking the right one to ask first about this snippet of code?"
What would be the most useful first question to ask?
/k1
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 5:12 PM (reply 11 of 57)



 
OK -

I am unable to predict the steps taking place in the "domatch(String in , String other)" method in the code given above.

Could you please explain how the recursion is taking place in that method?

-correct?

 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 5:13 PM (reply 12 of 57)



 
I'd disagree, for me the first question would be "what is it intended to do?"
 

Posts:357
Registered: 8/5/04
Re: Regex recursion explain   
Aug 7, 2004 5:16 PM (reply 13 of 57)



 
oh lol ok ok ...

its suppose to compare two strings given as command line argument...
and return true if the two strings MATCH and false if they don't
like this::::

java Regex my*bomberbllah my*bomberbllah

this will return TRUE

 

Posts:826
Registered: 7/1/03
Re: Regex recursion explain   
Aug 7, 2004 5:18 PM (reply 14 of 57)



 
OK -
Step by step.

Next question - why not use the existing, tested platform libraries to do that instead of wasting time on twisted code?

/k1
 
This topic has 57 replies on 4 pages.    1 | 2 | 3 | 4 | Next »