Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

regex problem

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Pie
Posts: 21216
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Pie
Posts: 21216
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Pie
Posts: 21216
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
    Bookmark Topic Watch Topic
  • New Topic