使正则表达式不匹配空字符串? [英] Make regular expression not match empty string?
问题描述
我的正则表达式在我的应用程序中运行良好,但它也匹配空字符串,即当输入为空时不会发生错误.如何修改此正则表达式,使其不匹配空字符串?请注意,我不想更改此正则表达式的任何其他功能.
I have regex which works fine in my application, but it matches an empty string too, i.e. no error occurs when the input is empty. How do I modify this regex so that it will not match an empty string ? Note that I DON'T want to change any other functionality of this regex.
这是我使用的正则表达式:^([0-9\(\)\/\+ \-]*)$
This is the regex which I'm using: ^([0-9\(\)\/\+ \-]*)$
我自己对正则表达式的了解并不多,这就是我问的原因.我一直在寻找答案,但找不到直接的答案.我最接近的是:正则表达式除了 c# 中的空字符串 之外的任何东西,但这对我来说真的不起作用..
I don't know a lot about regex formulation myself, which is why I'm asking. I have searched for an answer, but couldn't find a direct one. Closest I got to was this: regular expression for anything but an empty string in c#, but that doesn't really work for me ..
推荐答案
将*"替换为+",因为*"表示0次或多次出现",而+"表示至少出现一次"
Replace "*" with "+", as "*" means "0 or more occurrences", while "+" means "at least one occurrence"
这篇关于使正则表达式不匹配空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!