简体   繁体   English

正则表达式.NET匹配所有内容,直到找到第一个空格+字母

[英]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. 请帮助我使用正确的正则表达式以实现目标。

Instead of trying to replace, match what you want with: 与其尝试替换,不如将其匹配:

(?<= )[A-Za-z].*

demo 演示

Search for: 搜索:

^.*?- (?=[A-Za-z])

and replace by empty string. 并替换为空字符串。

RegEx Demo 正则演示

If you are looking for a regex solution that will only match the substrings after the last - , you can just use 如果你正在寻找一个正则表达式的解决方案,只会在最后匹配字符串-你可以只使用

-\s*([^-]*$)

See demo 观看演示

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字母。

在此处输入图片说明

C# IDEONE demo : C#IDEONE演示

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.

相关问题 RegEx匹配URL中第一个反斜杠后的所有内容 - RegEx to match everything after first backslash in URL 什么是用于匹配最后一个冒号和之前的第一个空格之间的所有内容的正则表达式? - What is a regex for matching everything between last colon and the first space before it? 启用IgnorePatternWhitespace时,.NET正则表达式匹配空格字符 - .NET Regex match space character when IgnorePatternWhitespace is on 匹配正则表达式中的字符(包括换行符)直到下一个匹配 - Matching characters (inc. newlines) in a regex until next match is found 正则表达式以匹配数字到字母的过渡,因此我可以在它们之间放置空格 - Regex to match digit to letter transition so i can put space between them RegEx:如果未找到特定模式,则匹配第一行或唯一行的模式 - RegEx : Pattern to match the first or only line if specific pattern is not found RegEx匹配两个单词首次出现之间的所有内容,这两个单词都出现多次 - RegEx to match everything between first occurrence of two words, both of which appear more than once 正则表达式匹配模式加上字符串的 rest 直到下一个点、逗号或空格 - Regex match pattern plus rest of the string until next dot, comma or space 正则表达式(.NET)捕获除空间以外的所有内容 - Regex (.NET) Capture Everything But Spaces 如何仅使用.Net Regex.Replace替换第一个匹配项 - How to replace the first match only using .Net Regex.Replace
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM