• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What should be Regex??

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to try Pattern.CASE_INSENSITIVE
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this may solve your problem?

[ April 28, 2008: Message edited by: Frank Eichfelder ]
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic