Regular expressions in C# to detect more than one space between words
I need to use regular expressions in C# to validate a TextBox. I need to add this role in ValidationExpress for a Validation Control 开发者_StackOverflowin ASP.NET. Regular expressions should not allow this:
- more that ONE space between words
- considering also beginning and end of the string
Any ideas?
No need for regexps.
if (string.StartsWith(" ") || string.EndsWith(" ") || string.Contains(" ")) throw...
If you are limited to validating a string, try the following pattern (example):
^[ ]?([^ ]+[ ])*[^ ]*$
It doesn't allow strings with two spaces anywhere in the string. This pattern ignores tabs and newlines, by the way. I've picked [ ]
so you can see the spaces, but a simple space is the same. \s
may not be right for you. For one, it might match a windows new line, \r\n
.
Similarly, you can use a negative lookahead (example):
^(?!.*[ ]{2})
If you're using a client side validator you need to match from start to end, so use the pattern (?!.*[ ]{2}).*
. It implicitly adds ^...$
around your pattern.
Either way, consider using a custom validator and writing a simple line of code to negate searching for two spaces. Here's how it's done. First, look at the documentation add a JavaScript function to your page:
function noTwoSpaces(source, arguments) {
arguments.IsValid = (arguments.Value.indexOf(' ') == -1);
}
Next, add the CustomValidator
control to use it:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage=":-("
ClientValidationFunction="noTwoSpaces"></asp:CustomValidator>
And that's it. Much easier than an elusive regex.
I believe you want:
if (myText.Split(" ", StringSplitOptions.RemoveEmptyEntries).Length !=
myText.Split(" ").Length)
{
//String contains multiple spaces
}
As you've now said you DO want regexes I'd use
@"\s\s+"
to match two or more whitespaces or:
@"\ \ +"
to match two or more spaces.
var rex = new Regex(@"\s{2,}");
Edit: Didn't see the start/end of the string.
string poo = "a b";
string poo1 = "a b";
string poo2 = "a b";
string poo3 = "a b";
string poo4 = " a b";
string poo5 = "a b ";
string poo6 = " a b ";
string poo7 = " a b ";
var rex = new Regex(@"^\s{0}.\s{0,1}.\s{0}$");
Console.WriteLine(rex.IsMatch(poo));
Console.WriteLine(rex.IsMatch(poo1));
Console.WriteLine(rex.IsMatch(poo2));
Console.WriteLine(rex.IsMatch(poo3));
Console.WriteLine(rex.IsMatch(poo4));
Console.WriteLine(rex.IsMatch(poo5));
Console.WriteLine(rex.IsMatch(poo6));
Console.WriteLine(rex.IsMatch(poo7));
This returns:
True False False False False False False False
Since the only valid string is the first one.
If you want to use regex, use this:
{2,}
(NB: There is a space before the brackets).
How about something like
string s = " tada bling zap ";
Regex reg = new Regex(@"\s\s+");
MatchCollection m = reg.Matches(s);
Can be done without regex, but imho it's less obvious to not use regex:
\s{2,}
or \s\s+
if .NET doesn't support the match-min-max syntax (I don't recall).
For beginning and ending no white spaces ^[^\s](.*\s.*)+[^\s]$
Mary bought a shoe
match
MarySqeezeIntoAshoe
no match
精彩评论