basic regex question

J

Jeff

....hoping someone can help someone still new to vb.net 2005 with something
new to him.

....been successfully using the regular expression validators from the
toolbox, but now I have need to do something strictly with code.

....need to determine if a stored string matches a stored regular expression.

....not sure what to search under for an example to give me a starting point.
I can figure out the actual expression syntax and the rest if someone can
get me started.

e.g., something along these lines

dim myregex as new regex
if mystoredstring = regex then
DoAllKindsOfStuff()
end if

Thanks

jeff
 
A

Andrew Morton

Jeff said:
...hoping someone can help someone still new to vb.net 2005 with
something new to him.

...been successfully using the regular expression validators from the
toolbox, but now I have need to do something strictly with code.

...need to determine if a stored string matches a stored regular
expression.

I think you're looking for the RegEx.IsMatch method.

Andrew
 
R

rowe_newsgroups

...hoping someone can help someone still new to vb.net 2005 with something
new to him.

...been successfully using the regular expression validators from the
toolbox, but now I have need to do something strictly with code.

...need to determine if a stored string matches a stored regular expression.

...not sure what to search under for an example to give me a starting point.
I can figure out the actual expression syntax and the rest if someone can
get me started.

e.g., something along these lines

dim myregex as new regex
if mystoredstring = regex then
DoAllKindsOfStuff()
end if

Thanks

jeff

Off the top of my head:

Imports System.Text.RegularExpressions

If Regex.IsMatch(mystoredstring, myregexpattern) Then
DoAllKindsOfStuff()
end if

Thanks,

Seth Rowe
 
J

Jeff

Okay, now I'm really confused.

I got this figured out somewhat, but either there is a major bug somewhere,
or I don't understand something fundamental (and it is likely the latter)

If I uses a regular expression validator from the toolbox and the expression
[a-zA-Z1-9]* it will properly match letters and numbers.

The exact same regular expression in the code below fails and matches
everything. Something seems to be going on everytime that I enter a reg
expression with a bracket - it will work with the toolbox validator, but not
when I use all vb code.

Can someone explain? I'm lost.

Dim regexobj As Regex = New Regex("[a-zA-Z1-9]*")

If RegexObj.IsMatch(TBUser.Text) = False Then
 
R

rowe_newsgroups

Okay, now I'm really confused.

I got this figured out somewhat, but either there is a major bug somewhere,
or I don't understand something fundamental (and it is likely the latter)

If I uses a regular expression validator from the toolbox and the expression
[a-zA-Z1-9]* it will properly match letters and numbers.

The exact same regular expression in the code below fails and matches
everything. Something seems to be going on everytime that I enter a reg
expression with a bracket - it will work with the toolbox validator, but not
when I use all vb code.

Can someone explain? I'm lost.

Dim regexobj As Regex = New Regex("[a-zA-Z1-9]*")

If RegexObj.IsMatch(TBUser.Text) = False Then

I'm just guessing - but it may have to do with the RegexOptions you
are using - try experimenting with them and see if it makes a
difference.

Also, you might want to post some sample text and the complete regex
pattern that is causing the problem - that way I can take a better
look at it.

Thanks,

Seth Rowe
 
J

Jeff

rowe_newsgroups said:
I'm just guessing - but it may have to do with the RegexOptions you
are using - try experimenting with them and see if it makes a
difference.

Also, you might want to post some sample text and the complete regex
pattern that is causing the problem - that way I can take a better
look at it.

Thanks,

Seth Rowe


Okay, I think that I have much of this figured out. The validator through
the toolbox (is there a good way to say this?) does not evaluate empty
textbox values regardless of the regex expression, while the hardcoded
version works essentially like all of the instructions that I see about
using regex. I didn't pick up on that initially, and that was causing the
confusion.

I think that I have it going now.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

basic regex quesiton. 2
regex Question 1
Regex Problem 1
Non matching regex? 1
Regular Expression Insanity 1
Newbie question about Regex 8
Another question about regex (not understanding) 5
Regex Output to Textbox 4

Top