tags:

views:

100

answers:

6

Hello, I have a path like this one:

C:\Development.TFS\Examn\R4-branch\Web\OrganisationManager\themes\blue\css

And I need a regex to get the word "blue" out of there. I tried, but didn't find a solution yet. It's practically getting the word before the last word from that string. Please help, thank you

+2  A: 

It is not a good idea to parse paths with regular expressions - it's much better to use a specific library for that purpose. What language are you using?

Eli Bendersky
I agree with you, but this isn't an answer.
Rubens Farias
@Rubens, I'm trying to teach fishing, not just give a fish
Eli Bendersky
I'm using C#, and I found a better solution for this, rather than using regex. You're right, it is better to use a specific library for that purpose, but first I also needed the regex to make some tests. Thank you anyway
Bogdan Craciun
@Eli, you can do that as a comment;
Rubens Farias
A: 

You should find this pattern in string: ([\w\s.-]+)\\[\w\s.-]+$. The first group will containg word 'blue' in your case. Exact syntax of regex and accessing groups depends on your programming language.

Rorick
`\d` is a subset of `\w`, so `\w\d` is redundant.
Amarghosh
Yes, you're right.
Rorick
+4  A: 
(\w+)\W+\w+$

matches the second-to-last word, captures it into backreference no. 1, then matches one or more non-word characters, then the last word and then EOL.

If you don't really want actual words but path elements to match (even if they contain non-word characters), then

([^\\:]+)\\[^\\]+$

might do a better job.

Edit: Added a : to the "second-to-last-word group" so the regex can handle relative paths, too.

Tim Pietzcker
Path elements contains non-word characters, e.g. 'R4-branch' contains '-'. So your regex won't match 'C:\Development.TFS\Examn\R4-branch\Web\OrganisationManager\themes\blue-sea\css'
Rorick
You're right of course. The OP asked for words, though, not path elements (which he probably meant). Will edit.
Tim Pietzcker
+1 for the updated regex.
Amarghosh
Yes, this is nicer! It will even correctly work with spaces unlike mine =)
Rorick
Thank you! Indeed, I actually wanted that path element, but your answer helped a lot :)
Bogdan Craciun
But won't work with relative paths: 'C:blue\css' =)
Rorick
Right again. Well, the more specific the requirements, the better the regex.
Tim Pietzcker
A: 

if (eregi('themes\\([a-z].+)\\css', $subject)) { # Successful match } else { # Match attempt failed }

A PHP example to get the word blue.

Marco
Maybe you must try regex buddyit is a great tool to make regexes
Marco
A: 

can't you simply split the string using the character '\' then get the splitResult[splitResult.Count-1]?

you could always replace '\' by the path separator in your environment, for more consistent results

Sam Holder
A: 

Another option:

(\w+)\\\w+$
Alix Axel
I posted this and deleted as the "words" in OP's path can include non `\w` characters like `-`,`.`,`space` etc.
Amarghosh