This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

regex confusion  RSS feed

 
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");
 
author
Posts: 23832
140
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
Posts: 23832
140
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
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!