wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to find only name of file(s) using regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find only name of file(s) using regular expression " Watch "How to find only name of file(s) using regular expression " New topic
Author

How to find only name of file(s) using regular expression

aniruddha panadare
Greenhorn

Joined: Dec 23, 2008
Posts: 15
import java.util.regex.*;
import java.util.Scanner.*;

class string
{
public static void main(String args[])
{
Pattern p=Pattern.compile("p\\w[\\.\\w]");
Matcher m=p.matcher("prg1.java,prog2se.txt,gprgtdd.pdf,jpr.dll,q.dl");
//index 0123456789012345678901234567890123456789012345

boolean b=false;
while(b=m.find())
System.out.print(m.start()+"-- "+m.group()+"\n");
}
}


OUTPUT===>

0-- prg
10-- pro
23-- prg
30-- pdf
35-- pr.

I want to find only name of file without extent :roll: ion
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

You pattern....



Matches 3 characters. The first must be the letter p. The second can be any word character. And the third can be either a period or a word character. This means that it will match these...

Matcher m=p.matcher("prg1.java,prog2se.txt,gprgtdd.pdf,jpr.dll,q.dl");

Which is what you are getting...
Henry


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

Joined: Oct 16, 2008
Posts: 952
I did not get exact file name, but this pattern is near to your result:



SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

A pattern like this...



is probably closer to what you want... (BTW, I haven't actually tried it, so take with a grain of salt.)

Henry
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes it is working Henry.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
"\\w+(?=\\.)" even this is also working, but this is out of scjp topic, as we have to use positive lookahead (?=) here.

\\w+(?=\\.)

Henry, does it means start with any word character and end with \\. but do not include \\. in result.
Does positive lookahead ?= used for exclusive ending boundary.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18531
    
  40

The extra backslash blackslash period in the beginning is needed because this...

this.is.a.valid.filename.pdf

is a valid name of a file. You want to only remove the extension -- not everything else.

Henry, does it means start with any word character and end with \\. but do not include \\. in result.
Does positive lookahead ?= used for exclusive ending boundary.


Basically yes. A positive lookahead means to look to match it, but it does not count in the result. It is great for looking for boundaries, where the boundary is related to the next match. Or delimiters.

Henry
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This does not sound like it is about the SCJP exam. Please post your questions in the correct forum. I'm moving this to a more appropriate forum for you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find only name of file(s) using regular expression
 
Similar Threads
Taglib that translates into a series of other taglib calls
The import org.apache.xmlrpc cannot be resolved
How to import CSV file into Mysql using jsp
parsing data and storing in the xml
Fake a post request from java class