Regular Expression validate textbox

mojoman

Member
Joined
Nov 21, 2006
Messages
20
Programming Experience
Beginner
Hello,

I know regular expressions can be created in vb.net but the resources are scare on the net and I cannot find examples. I need to create a regular expression that ensures that at least two words have been entered and the words can just have letters in them. Can someone tell me how to do that and how to submit a string to the regular expression for checking.

As well, a few additional examples would be good so I can learn how they are done.

Thank you,
 
I was looking more for sample vb.net code as I am new. Can someone show me how to code to search for two words case insensitively?
 
Hello,

I know regular expressions can be created in vb.net but the resources are scare on the net and I cannot find examples. I need to create a regular expression that ensures that at least two words have been entered and the words can just have letters in them. Can someone tell me how to do that and how to submit a string to the regular expression for checking.

As well, a few additional examples would be good so I can learn how they are done.

Thank you,

MSDN has all the infor you should need:

http://msdn2.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

The regex you have asked for is:

"\w+ \w+"

at least one/at most infinite number of any word character, followed by space, followed by at least one/at most any number of word characters
 
I was looking more for sample vb.net code as I am new. Can someone show me how to code to search for two words case insensitively?



Oh, its something like:

VB.NET:
Imports System.Text.RegularExpressions

Public Function ContainsTwoWords(input as String) as Boolean
  Return Regex.IsMatch("\w+ \w+", input)
End Function

Note that if you only want your input string to contain two words, you need to add start and end markers:

"%\w+ \w+$"

Without these, so long as input contained any two words anywhere, the two words would be matched:


38742987353hello world..32.4.2345.2435
 
'^' is the start marker, '%' is not known to me as anything special. '\w' is word characters letters and digits. I would define a word where digits is not allowed as "[^\d\s]+", this creates a character class that excludes digits and whitespace and let it repeat one or more times '+'. To enable 'wording' add an optional space ' ?' to it and make this a group '(...)' you repeat two or more times '{2,}'. Finally limit the expression with start-of-string '^' and end-of-string '$'. The expression would then be "^([^\d\s]+ ?){2,}$". That looks pretty good? :) but the optional space will not ensure two words in fact, and making the space mandatory you will have to postpend every word with space which is not realistic.

If we combine cjards suggestion with 'only letters', make it a repeating group I think it will be fine:
VB.NET:
Dim regTwoPlusWords As String = "^([^\d\s]+ [^\d\s]+)+$"

You also have to swap the parameters from cjards example: Regex.IsMatch(input, pattern)
 
Not sure...

I am afraid I do not understand COMPLETELY the logic of what is going on:

Dim regTwoPlusWords As String = "^([^\d\s]+ [^\d\s]+)+$"

Why can't we just write ([^\d\s]+ [^\d\s]+)

Is it because that would just mean we can have just TWO WORDS?
 
Yes. '+' repeating that group allows more than two words. '^' and '$' limits to start and end of string, else you could write "3541!# two words 9451#¤%" and it would match because this string contains "two words" that matches the pattern.
 
Forgot something :eek: [^\d\s] excludes digits and whitespace but will include letters and also special characters (like puntuation !?.,) - to avoid this you have to take the word class and exclude digits [\w-[\d]], Now we're getting to this:
VB.NET:
Dim regTwoPlusWords As String = "^([\w-[\d]]+ [\w-[\d]]+)+$"
Hope it solves this puzzle now ;)
 
Help With Regular Expression

Hello,

I have to create a regular expression with the following conditions:

1) Must be composed of words
2) Words must be composed STRICTLY of letters (no numbers or other characters allowed)

2)Must be composed of a minimum of two words and the first two words must have a minimum of three letters.

Thus far I have managed the following:

^([a-zA-Z]{3}[a-zA-Z]* [a-zA-Z]{3}[a-zA-Z]....

after the three dots I am not sure how to continue...

Any help would be appreciated.
 
Modify the above to set repeat of the first two character classes from required 3 to infinity, then add a star optional class that allow letters or space:
VB.NET:
Dim regTwoPlusWords As String = "^[\w-[\d]]{3,} [\w-[\d]]{3,}[ \w-[\d]]*$"
Btw, your a-Z class is perhaps the only letters you know, but for the rest of the world that is a small and exclusive group of english-only letters.
 
Back
Top