aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regex problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

regex problem

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
class C6{
public static void main(String[] a){
String s="abc de.f1 adf34 cat.dog";
System.out.println(s+"\n");
String[] t=s.split(a[0]);
for(String x:t)
System.out.println("<"+x+">");
//System.out.println(t[0]);
}
}
/*

C:\code\e5> javac C6.java


1. C:\code\e5>java C6 .
abc de.f1 adf34 cat.dog

2 C:\code\e5>java C6 \.
abc de.f1 adf34 cat.dog

<abc de>
<f1 adf34 cat>
<dog>


3. C:\code\e5>java C6 \\.
abc de.f1 adf34 cat.dog

<abc de.f1 adf34 cat.dog>


*/
Kindly explain why these three invocations behave this way. especially the 1st one.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

ishusharma sharma wrote:Kindly explain why these three invocations behave this way. especially the 1st one.


Why not take a shot at it? Explain to us what you think is happening, and we can guide you in the right direction.

BTW, save the first example for last -- there is something else going on that is different from the other two.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Henry Wong wrote:
ishusharma sharma wrote:Kindly explain why these three invocations behave this way. especially the 1st one.


Why not take a shot at it? Explain to us what you think is happening, and we can guide you in the right direction.

BTW, save the first example for last -- there is something else going on that is different from the other two.

Henry

I think that the 2nd one used escape character to tell the jvm that dot(.) is not a metacharacter. But I have no idea why did other two behaved like this. Please give a quick response.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

ishusharma sharma wrote:I think that the 2nd one used escape character to tell the jvm that dot(.) is not a metacharacter. But I have no idea why did other two behaved like this. Please give a quick response.


In order to understand the explanation, you need at least a basic understanding of regular expressions. From your response, this is not clear (or at least, it doesn't come across as such) -- with the exception that you know that a "dot" is some sort of "metacharacter", I am unsure of your unstanding of regular expressions. Perhaps it would be a good idea to start with the tutorial.

http://docs.oracle.com/javase/tutorial/essential/regex/

Henry
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Henry Wong wrote:
ishusharma sharma wrote:I think that the 2nd one used escape character to tell the jvm that dot(.) is not a metacharacter. But I have no idea why did other two behaved like this. Please give a quick response.


In order to understand the explanation, you need at least a basic understanding of regular expressions. From your response, this is not clear (or at least, it doesn't come across as such) -- with the exception that you know that a "dot" is some sort of "metacharacter", I am unsure of your unstanding of regular expressions. Perhaps it would be a good idea to start with the tutorial.

http://docs.oracle.com/javase/tutorial/essential/regex/

Henry

Maybe I don't have understanding of regular expression.Now can you kindly explain me the reasons of the behavior of the code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Topic locked, because a duplicate topic was started here...

http://www.coderanch.com/t/595328/java-programmer-SCJP/certification/Dot-metacharacter
 
wood burning stoves
 
subject: regex problem