简体   繁体   English

正则表达式选择[AZ],但连续两个空格除外

[英]Regex Select [A-Z ] except two consecutive spaces

I've been trying to create a regex to match all letters from AZ and a single space. 我一直在尝试创建一个正则表达式来匹配AZ和一个空格中的所有字母。 I want it to stop when it becomes more than 1 consecutive space. 我希望它在超过1个连续空间时停止。 I also do not want the dash included. 我也不希望包含破折号。 This is being called and returned as a string in a C# based program. 这是在基于C#的程序中作为字符串调用并返回的。 I plan to just crop the string at the end by 1 after I have imported it to remove the space at the end (if it ends up being returned by regex). 我计划在导入字符串后将其尾部裁剪成1,以删除尾部的空格(如果最终由正则表达式返回)。

TERRA MARVELLOUS LUX      -

So far I've tried a multitude of expressions the best I can come up with so far is 到目前为止,我已经尝试了多种表达式,到目前为止我能想到的最好的方法是

^[A-Z (?!\s{2,})]*

as well as 以及

^[A-Z ]*(?:\s{2})

and so on I could list more I've been trying. 等等,我可以列出更多我一直在尝试的内容。 I'm quite stuck. 我很困。

^[A-Z ]+\b

You can try this.See demo. 您可以尝试一下。请参阅演示。

https://regex101.com/r/tJ2mW5/20 https://regex101.com/r/tJ2mW5/20

This should solve your issue: 这应该可以解决您的问题:

([A-Z]+[ ]{1})+

The main idea is to match all the groups of at least one character of AZ, then have one space and match the pattern at least one time. 主要思想是匹配至少具有AZ个字符的所有组,然后具有一个空格并至少匹配一次模式。

And the example: https://regex101.com/r/tJ2mW5/21 和示例: https : //regex101.com/r/tJ2mW5/21

If you want to just strip final spaces with hyphen, you can use 如果您只想用连字符去除最后的空格,可以使用

 var t1 = "TERRA MARVELLOUS  LUX      -";
 var rx2 = new Regex(@"\s+\-?$");
 var res = rx2.Replace(t1, string.Empty);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM