How to ignore comments in regular expression???

Joined
Feb 27, 2008
Messages
1
Reaction score
0
Hi,

Im creating program that reads a file content and search for a URL. While searching i do not want to take url's present inside the comment(wanna regex to ignore comment(//)).For that

i have written source code but its not working out..Can anyone check and help me out..

This is my source code:

import java.io.*;
import java.util.regex.*;
import java.util.*;
public class sam
{


public static void main(String args[]) throws IOException
{
String wordd;
String word;
String path="(ftp|http|file|gopher|telnet|news):(//){1}([^/]+)(/.*)?(/.*)";
Pattern p=Pattern.compile(path,Pattern.COMMENTS);
LineNumberReader fr=new LineNumberReader(new FileReader("javatest.java"));
while((wordd=fr.readLine())!=null)
{
Matcher m=p.matcher(wordd);
while(m.find())
{
word=m.group();
System.out.println("Line"+fr.getLineNumber()+":"+"\t"+wordd);
}
}
}
}

This is the file im trying to get URL:

JavaTest.java:

cdhchukhjkcdhncks
//htrhhhth"D:\maintest\test"
sdgudhudhu
//htrhrthrhht"c:\"
dhdijijdeil
ijidjsa
"a:\maintest"
"A:\test"
"t:"
"t:
/*
"a:\maintest"
"A:\test"
"a:\maintest"
"A:\test"
"a:\maintest"
"A:\test"
*/hisfdsfffd
htrhrthrhht"c:\"
"D:\maintest\test"
/*


When i run the above program output is like this:

C:\jdk1.5.0_08\bin>javac sam.java

C:\jdk1.5.0_08\bin>java sam
Line25: fhfdihhhhh"file://www.raja.com/fdf/vcdv.html"
Line26: "http://www.raja.com/fdf/"
Line27: "ftp://www.raja.com/fdf"
Line28: "telnet://www.raja.com/"
Line30: "http://www.raja.com/fdf/"
Line31: "ftp://www.raja.com/fdf"
Line32: //"telnet://www.raja.com/"
Line33: //"news://www.raja.com/"
Line34: //"file://www.raja.com/fdf/vcdv.html"
Line35: /*"file://www.raja.com/fdf/vcdv.html"
Line36: "file://www.raja.com/fdf/vcdv.html""file://www.raja.com/fdf/vcdv.html"*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top