• 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

regex confusion

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic