C# 正则表达式 - 匹配后只允许有空格或没有空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10211815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex - only allow a space or nothing after a match
提问by gezequiel
Hi I'm nearly new with this of Regex... so, maybe my problem it's easy but I can't find a solution!
嗨,我对 Regex 几乎是新手……所以,也许我的问题很简单,但我找不到解决方案!
I'm writing a regex pattern that looks into the user's writing and paint with another color the matches that is founded. I want for example to paint with another color, if the user write something like this:
我正在编写一个正则表达式模式,它查看用户的写作并用另一种颜色绘制已建立的匹配项。例如,我想用另一种颜色绘画,如果用户写这样的东西:
foo()
the thing is that I DON'T want to paint that if the user writes something else after that, I mean if the user write only
问题是我不想画,如果用户在那之后写了其他东西,我的意思是如果用户只写
"foo()" (or "foo() ")
then it's fine, I want to paint it, but if the user write
那么没关系,我想画它,但如果用户写
"foo()d"
I don't want to paint that because is now well written for me.
我不想画那个,因为现在对我来说写得很好。
I already wrote the regex pattern that match the "foo()" (or also with a dot in the middle, like "foo.foo()"), but I′m facing with that problem. I need to add something to my pattern that allow only a space, or nothing (if the user write something else after the ")" I don't want to match it.) This is my pattern:
我已经编写了与“foo()”匹配的正则表达式模式(或者中间还有一个点,比如“foo.foo()”),但我正面临着这个问题。我需要在我的模式中添加一些只允许一个空格或什么都不允许的内容(如果用户在“)”之后写其他内容,我不想匹配它。)这是我的模式:
[a-z]*\.?[a-z]*\(("[^"\r\n]*"|[-]?\b\d+[\.]?\d*\b)?\)
Thank you very much!
非常感谢!
采纳答案by Ade Stringer
David Brabant is close, but I think you actually want to try ending your regular expression with (?!\S)- this will mean you'll match anything not followed by a non-whitespace character. If you just want to match on spaces rather than whitespace, use (?![^ ]).
David Brabant 很接近,但我认为您实际上想尝试用以下方式结束您的正则表达式(?!\S)- 这意味着您将匹配后面没有非空白字符的任何内容。如果您只想匹配空格而不是空格,请使用(?![^ ]).
回答by Scen
[a-z]*\.?[a-z]*\(("[^"\r\n]*"|[-]?\b\d+[\.]?\d*\b)?\)[ ]?
Adding a [ ]?should do it. ?is used for 1or 0, [ ]will only match space.
添加一个[ ]?应该可以。?用于1or 0,[ ]只会匹配空格。
Also, [\s]?would work for all types of whitespace (tabs included).
此外,[\s]?适用于所有类型的空格(包括制表符)。
回答by James Johnson
This seems to do what you're looking for according to my regex tester:
根据我的正则表达式测试仪,这似乎可以满足您的要求:
[a-z]*\.?[a-z]*\(("[^"\r\n]*"|[-]?\b\d+[\.]?\d*\b)?\)([ ]?)(?!.)
If you want to allow for more than one space, use this:
如果您想允许多个空格,请使用以下命令:
[a-z]*\.?[a-z]*\(("[^"\r\n]*"|[-]?\b\d+[\.]?\d*\b)?\)([ ]*)(?!.)
回答by David Brabant
Use negative look ahead:
使用负面展望:
(\w+)(\.*)(\(\))+(\s)*(?!.)
The important part for you in the regex above is: (\s)*(?!.)
在上面的正则表达式中对您来说重要的部分是:(\s)*(?!.)
(\s)* : followed by 0 or more white spaces (?!.) : and no other character
(\s)* : 后跟 0 个或多个空格 (?!.) : 没有其他字符
回答by stefano m
could you try to add:
你可以尝试添加:
\s*% ?
\s*% ?
\s* : zero or more spaces
\s* : 零个或多个空格
% means: end of string
% 表示:字符串结束

