[英]Regex .NET match everything until first space + letter is found
I have: 我有:
01 - Radio N Am 2007
186508980X -16-17 - Horns and Bones
(ab)normal - constitutions
Goal: 目标:
Radio N Am 2007
Horns and Bones
constitutions
I tried with 我尝试过
^(?:(?!(?:\S*[\s[a-z][A-Z])).)+
but the output is: 但输出是:
- Radio N Am 2007
- Horns and Bones
Please help me with a correct regex to achieve the goal. 请帮助我使用正确的正则表达式以实现目标。
If you are looking for a regex solution that will only match the substrings after the last -
, you can just use 如果你正在寻找一个正则表达式的解决方案,只会在最后匹配字符串
-
你可以只使用
-\s*([^-]*$)
If there must be a letter after the hyphen , you may use -\\s*(\\p{L}[^-]*$)
. 如果连字符后面必须有一个字母,则可以使用
-\\s*(\\p{L}[^-]*$)
。 The \\p{L}
construct will match any Unicode letter. \\p{L}
构造将匹配任何Unicode字母。
var lines = new string[] {"01 - Radio N Am 2007",
"186508980X -16-17 - Horns and Bones",
"(ab)normal - constitutions"};
foreach (string s in lines)
{
var matches = Regex.Matches(s, @"-\s*([^-]*$)");
foreach (Match m in matches)
Console.WriteLine(m.Groups[1].Value);
}
But you can also use a non-regex approach if you need to get substrings after the last hyphen: 但是,如果需要在最后一个连字符后获取子字符串,也可以使用非正则表达式方法:
Console.WriteLine(s.Substring(s.LastIndexOf("-") + 1).Trim());
See another demo 观看另一个演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.