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

regex confusion

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can someone help me to find out exact pattern in the following case ?
I made one program which searched out all the files in my computers and made one csv file..!!!
I did this successfully..so I know that in my system I have a lot of movies..
I want to list out all the movies name from that file..!!
movie name can have either divx or avi extension..!!
I dont know what should be the pattern to search..?
I tried following ;


but doesn't work...giving me out put at
[code]
---------- JAVA ----------
c.avi,
a.avi,
l.avi,
g.avi,

Output completed (0 sec consumed) - Normal Termination
[code]

here is the code where i am searching that;
[code]
Matcher m = p.matcher(line);
boolean b= false;
while((b = m.find()))
{
System.out.println(m.group());

}
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi just to correct my self..
output is Obtained If I change the pattern to

Pattern p = Pattern.compile("[^,]([.])*avi");
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sunny Jain:
Hi just to correct my self..
output is Obtained If I change the pattern to

Pattern p = Pattern.compile("[^,]([.])*avi");



I have a question -- what was the reasoning behind this pattern? The pattern you posted just seem to match by luck... basically, your pattern is...

A single character that is not a comma, followed by zero or more periods, and finally followed by "avi". Furthermore, your definition of the period seems cumbersome -- it is a group of a character class which has a single character which is the period.

Henry
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi [.] here means that any character could server here, I dont know what can be the name of file...so i used dot, I used * because I dont know the number of character in the name of file..let me tell you what i want to print:


suppose my line contian the following:
before sunrise,a walk to remember,ocean -13.avi,tokyo drift.divx,basic instinct 2,before sunset,cast away,gal next door.avi,bourne identity



now i want the following output:
ocean -13.avi
gal next door.avi

what should be the regex pattern for this?
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi [.] here means that any character could server here, I dont know what can be the name of file


This is not true. A period in a character class means a period. It doesn't mean any character. If you want to mean any character, then get rid of those square brackets.

what should be the regex pattern for this?


With the hint that I just provided, I'll let you try and fix it first.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic