$filename =~ s/^.*[\/\\]([^\/\\]+)$/$1/;

Page 3 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: numark
Originally posted by: BingBongWongFooey
$filename =~ s/^.*[\/\\]([^\/\\]+)$/$1/;

filename = re.sub(r'^(.*)[/\]([^/\]+)$', r'\1', filename)

python > perl :D

I don't trust any language that determines for me the way I have to format my code. I'll tab where I want to tab, d@mn it! :D

i'd rather have it that way then have ugly { } 's everywhere. less syntax == less bugs, less headaches.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: jliechty
Originally posted by: Ameesh
Originally posted by: notfred
regular expressions are one of the best things ever.
CFG >> Regular Expressions
Huh? I googled for CFG and didn't find anything that looked relevant, except for some C header file processor thing on the 3rd or 4th page. Oh well, what Ameesh says is not going to deter me from continuing in my journey to learn regular expressions, since he only says they are not the best simply because he has an allergic reaction to all things open source.
rolleye.gif

CFG = Context free grammers

IMO all prgrammers should know how to use regular expressions.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Descartes
For someone that loves Perl and regular expressions so much, one would think you'd craft an efficient expression :)

Why bother w/ the superfluous '.' and the greedy quantifier? ([^\/\\]+)$ would work just as nicely.

I'll let you figure out why that doesn't work on your own :)
 

The Dancing Peacock

Diamond Member
Dec 22, 1999
3,385
0
0
Originally posted by: Descartes
For someone that loves Perl and regular expressions so much, one would think you'd craft an efficient expression :)

Why bother w/ the superfluous '.' and the greedy quantifier? ([^\/\\]+)$ would work just as nicely.

not if there was 0 or more characters between the beginning of the line and that first slash.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: The Dancing Peacock
Originally posted by: Descartes
For someone that loves Perl and regular expressions so much, one would think you'd craft an efficient expression :)

Why bother w/ the superfluous '.' and the greedy quantifier? ([^\/\\]+)$ would work just as nicely.

not if there was 0 or more characters between the beginning of the line and that first slash.

Uhh, yes it will. The whole point is that it doesn't look for any characters at the beginning, the engine first anchors to the end of the line, then backtracks to the first \ or /.

I'll let you figure out why that doesn't work on your own :)

YGPM :)
 

The Dancing Peacock

Diamond Member
Dec 22, 1999
3,385
0
0
Originally posted by: Descartes
Originally posted by: The Dancing Peacock
Originally posted by: Descartes
For someone that loves Perl and regular expressions so much, one would think you'd craft an efficient expression :)

Why bother w/ the superfluous '.' and the greedy quantifier? ([^\/\\]+)$ would work just as nicely.

not if there was 0 or more characters between the beginning of the line and that first slash.

Uhh, yes it will. The whole point is that it doesn't look for any characters at the beginning, the engine first anchors to the end of the line, then backtracks to the first \ or /.

I'll let you figure out why that doesn't work on your own :)

YGPM :)


s/^.*[\/\\]([^\/\\]+)$/$1/;

Notfred, correct me if I'm wrong, but here's how I read it. It is anchored at the beginning by the ^. It's looking for 0 or more anything between beginning of the line and the first /, followed by
a / then a \. He's capturing every character after that is not a / or \ until the end of the line.

Your regex is capturing anything that is not / or \ anchored at the end of the line.

He is looking for specifc data in a particular format with his..