tags:

views:

104

answers:

3

I need a regular expression that will match the first letter of a song title without articles like "the", "an", "a". I'm writing a custom import script for Mediatomb which uses javascript. I need to be able to put songs in alphabetical folders.

Example: "Panama.mp3" would be in folder "P", "The Gambler.mp3" would be in folder "G"

+1  A: 

Not sure what flavour of regex you're using but there are: non-capture groups you miught be able to use it like this:

(?:(the |a |an ))([a-zA-Z])

Capture the 3rd group and that should always be the first letter (excluding the "the, a, an,...".

Edit: Meant to say capture the SECOND group for that first letter. Also make sure you run this case-insensitive. And get a good regular expression test tool (I like Expresso, but there's others).

Edit2: made some refinements ;) (?:(the|a|an) +)?([a-zA-Z0-9])

FrustratedWithFormsDesigner
Your regex there _requires_ the leading article. You might want to stick a ? after the non-capture group.
Kevin Ballard
also the regex requires exactly one space - may be better to include any whitespace
peter.murray.rust
Thanks for the hints I think I got one that works.(?:(the |a |an ))*(\S{1})(\S*)
Wili
A: 

Thanks to the answer above, this is what I came up with. Let me know if there's any way to improve upon it.

(?:(the |a |an ))*(\S{1})(\S*)
Wili
You may want to add a ^ at the begining to match begining of line/string. Also, there COULD be more than one space after the article, so I'd re-write that article-capture part as ^(?:(the|a|an) +)
FrustratedWithFormsDesigner
You have two sets of parentheses around the list of articles. Only one is marked as non-capturing. Thus, the first letter will appear in the 2nd group. Also, you don't need `{1}`. Furthermore, what's the point of the `(\S*)`?
Jeremy Stein
A: 
var myregexp = /^(?:(?:the|a|an)\s+)?(\S)/i;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}
Jeremy Stein