This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
aniruddha panadare
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get exact file name, but this pattern is near to your result:

 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is working Henry.
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\\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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic