我需要一个正则表达式,将匹配歌曲标题的第一个字母没有像" the ","an","a“这样的文章。我正在为Mediatomb编写一个使用javascript的自定义导入脚本。我需要能够把歌曲放在字母顺序的文件夹中。
例如:"Panama.mp3“在文件夹"P”中,"The Gambler.mp3“在文件夹"G”中。
发布于 2009-11-10 09:38:36
多亏了上面的答案,这就是我想出来的。如果有任何方法可以改进,请告诉我。
(?:(the |a |an ))*(\S{1})(\S*)发布于 2009-11-10 09:14:14
不确定你使用的正则表达式是什么味道,但是有:non-capture groups,你可以这样使用它:
(?:(the |a |an ))([a-zA-Z])捕获第三组,它应该始终是第一个字母(不包括" the,a,an,...“。
编辑:意思是说捕获第一个字母的第二组。还要确保运行这个不区分大小写的命令。并获得一个好的正则表达式测试工具(我喜欢Expresso,但还有其他工具)。
Edit2:做了一些改进;) (?:(the|a|an) +)?([a-zA-Z0-9])
发布于 2017-01-26 03:29:15
Javascript示例-
const regex = /(?:(the|a|an) +)/g;
const str = `the cat in the hat a hare `;
const subst = ` `;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);非捕获组(?:(the|a|an) +)
第一捕获组(|a|an)
- matches the character literally (case sensitive)
- Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
- g modifier: global. All matches (don't return after first match)
https://stackoverflow.com/questions/1705057
复制相似问题