我希望你能够在没有空格的标题中找到并分离单词。
之前:
ThisIsAnExampleTitleHELLO-WORLD2019T.E.S.T.(Test)"Test"'Test'[Test]
之后:
这是一个示例标题 HELLO-WORLD 2019 T.E.S.T. (测试) [测试] "测试" '测试'
我正在寻找一个正则表达式规则,它能够完成以下任务:
如果一个单词以大写字母开头,则标识出该单词。
但是也要保留所有的大写单词,以免将它们空格成{{A L L U P P E R C A S E}}。
附加规则:
- 如果字母与数字相接触,则在它们之间加上空格:
Hello2019World
变成Hello 2019 World
- 忽略包含句点、连字符或下划线的缩写字母中的空格:T.E.S.T.
- 如果在括号、圆括号或引号之间,则忽略空格:[Test] (Test) "Test" 'Test'
- 保留连字符:Hello-World
C#
https://rextester.com/GAZJS38767
// Title without spaces
string title = "ThisIsAnExampleTitleHELLO-WORLD2019T.E.S.T.(Test)[Test]\"Test\"'Test'";
// Detect where to space words
string[] split = Regex.Split(title, "(?<!^)(?=(?<![.\\-'\"([{])[A-Z][\\d+]?)");
// Trim each word of extra spaces before joining
split = (from e in split
select e.Trim()).ToArray();
// Join into new title
string newtitle = string.Join(" ", split);
// Display
Console.WriteLine(newtitle);
正则表达式
我在数字、括号、圆括号和引号前面的空格上遇到了问题。
https://regex101.com/r/9IIYGX/1
(?<!^)(?=(?<![.\-'"([{])(?<![A-Z])[A-Z][\d+?]?)
(?<!^) // Negative look behind
(?= // Positive look ahead
(?<![.\-'"([{]) // Ignore if starts with punctuation
(?<![A-Z]) // Ignore if starts with double Uppercase letter
[A-Z] // Space after each Uppercase letter
[\d+]? // Space after number
)
解决方案
感谢您所有人的答案。这是一个正则表达式示例。我将其应用于文件名,并排除特殊字符\/:*?"<>|
。