permaculture playing cards*
The moose likes Beginning Java and the fly likes What should be Regex?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What should be Regex??" Watch "What should be Regex??" New topic
Author

What should be Regex??

Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
Hi Ranchers,
I have a file called file1.txt with following contents:

1 prabhatpankaj
2
3 select hello.pankaj("prabhat");
4 select hello.pankaj(prabhat);
5 select pack.prabhat ()
6 "select * From prabhat"
7 "select * From prabhat "
8 Prabhat
9 select hello.pankaj( prabhat );
10 select hello.pankajPrabhat;

here i have to search word prabhat (case-insensitive). the valid ocuurences are at line 3,4,5,6,7,8,9 but at line 1 and 10 it is not valid.

i have written a small java code for that like

public static void searchMethod(){
File file = new file (" C:\\Documents and Settings\\prabhat_kumar04\\Desktop\\Testing\\file1.txt");

FileReader fr =new FileReader(file);
BufferedReader br=new BufferedReader(fr);

//Setting the linenumber for searching.
int lineNumber=1;

String data= br.readLine();


while(data!=null)

{

data=data.toUpperCase();
Pattern p = Pattern.compile("[\\W]PRABHAT[\\W]");
Matcher matcher=p.matcher(data);
while(matcher.find()){

System.out.println(" prabhat is at line number :"+lineNumber);

}

// increment the line number
lineNumber++;

// read the next line of file and store into String "data".
data=br.readLine();

}

}

the problem is when i am calling this method it is not showing the line 8 as valid occurence.
Moreover if we write as

Pattern p = Pattern.compile("PRABHAT[\\W]");then it will take as line 8 and 10 bot valid one,whereas line 10 should be invalid.

Suggest the pattern that would fulfil the requirement.
With thanks in advance'

Prabhat
[ April 28, 2008: Message edited by: David O'Meara ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You probably want to try Pattern.CASE_INSENSITIVE
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
No David,
Problem is not with CASE-SENSITIVITY .Okies suppose we have all letters in small case and i have changed the code as

while(data!=null)

{

// data=data.toUpperCase();
Pattern p = Pattern.compile("[\\W]prabhat[\\W]");
Matcher matcher=p.matcher(data);
while(matcher.find()){

System.out.println(" prabhat is at line number :"+lineNumber);

}

and the line 8 in file file1.txt has been changed as
8 prabhat

now the problem has remained same i.e it wont count this one as valid.

Please suggest some regex for same.
Frank Eichfelder
Ranch Hand

Joined: Aug 25, 2003
Posts: 33
Perhaps this may solve your problem?

[ April 28, 2008: Message edited by: Frank Eichfelder ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What should be Regex??
 
Similar Threads
Problem with FileWrite and Regular Expression
Want to write a combined text file, but nothing happens..
Scanner class
problem in IO reader
declaring array of vectors