This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes regex confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regex confusion" Watch "regex confusion" New topic
Author

regex confusion

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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());

}


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi just to correct my self..
output is Obtained If I change the pattern to

Pattern p = Pattern.compile("[^,]([.])*avi");
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: regex confusion
 
Similar Threads
How to solve this problem
Servlet throws stackOverflowException
Mediaplayer framework and avi files
JSP Not able to Identify the JS, CSS file path
ImageJ Animator