• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2447
  • Last Modified:

regex to get filename without extension

Hi friends,

could you pls help me....how to get filename without extension using regex

ex:from this
/oracle/app/parent1/parent2/parent3/file1.xml

to this...I need only file1

pls note...parent folder hierarchy might change. it might have 4 parents or 5 etc. but we need only the file name without ext and dot.

Thanks in advance
0
enthuguy
Asked:
enthuguy
  • 4
  • 2
  • 2
  • +3
2 Solutions
 
farzanjCommented:
([^/.]+)\.[^.]*$

Open in new window

0
 
cmalakarCommented:
Here is a javascript regex.

var x = '/oracle/app/parent1/parent2/parent3/file1.xml';
var patt=/([^/]*)\.[^.]*$/;
var result=patt.exec(x);
alert(result[1]);
0
 
käµfm³d 👽Commented:
Neither approach takes into account the fact that filenames can consist of multiple dots. My suggestion would be to use a lookahead:

[^/]+(?=(?:\.[^.]+)?$)

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lwadwellCommented:
As a regex is greedy by nature ... this should work
   
^.*\/(.*)\.\w+$

Open in new window

0
 
farzanjCommented:
Ok, taking care of having no . or multiple
([^/.]+)(?:\.?[^/.])*$

Open in new window

0
 
enthuguyAuthor Commented:
Really pleased to see all your quick responses to help me out.

I use this online regex tester.
http://regexpal.com/
http://www.nregex.com/nregex/default.aspx

for some reason, all regex expression returned whole file name including extension.

but i need only file name without extension and dot. (sorry if I had confused you)
0
 
farzanjCommented:
0
 
enthuguyAuthor Commented:
thx farzanj, this link sees to be giving right output. I will try this tomo. in my script and let you know the outcome.
0
 
cmalakarCommented:
enthuguy,

Test my code completely.
There will be two matches in my regex. 1) result[0] and 2) result[1]
first one should be giving full path, where as second one gives the file name alone.
U should use result[1] to get the file name.

Online testers should be giving the first result, not the second one.
0
 
farzanjCommented:
The above tester shows both results
0
 
skullnobrainsCommented:
you should be able to rely on greediness, but if you are unsure about greediness or possible automagic docking of the expression, this should cover all cases

^.*/([^/.][^/.]*)\.[^.]*$

you need to prefix the ( and ) with a \ if your expression engine understands basic regular expressions instead of extended, perl or python regular expressions

please give points to farzanj as his very first answer was good enough
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now