467,122 Members | 1,273 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

RegEx question

I'm new to RegEx in vb.net so I'm not sure how to do this.

I want to know if a string contains two minus signs "-". If there are two
then I want it to return TRUE.

I also need to know if the string contains two plus signs "+". Should this
be a seperate RegEx or can one RegEx check for both signs?

Thanks!
Nov 21 '05 #1
  • viewed: 1512
Share:
8 Replies
Do the minus signs have to be right next to each other? Or can they be
separated by other text? If you simply want to find out if there are
more than one minus sign in a string, you don't need a regular
expression, you can use the string.split method:

Dim bMoreThanOne as Boolean

bMoreThanOne = (MyString.Split("-"c).Count > 2)

I'm not sure if this will perform any faster than a regular expression.
Usign an regular Expression you can do this:

Dim s As String = "ssdfsdf-sdfsdfsdfsdf-sdfsdfsdf-sdfsdf"

Dim rx As New Regex("-")
bMoreThanOne = (rx.Matches(s).Count > 1)

This should work if the string contains either - or + but I have not
tested it

Dim rx As New Regex(".*-.*-.*|.*\+.*\+.*")

Hope this helps

Nov 21 '05 #2
Wow! Either of those will work fine. They don't have to be next to each
other.

Thanks!

"Chris Dunaway" <du******@gmail.com> wrote in
news:11**********************@c13g2000cwb.googlegr oups.com:
Do the minus signs have to be right next to each other? Or can they be
separated by other text? If you simply want to find out if there are
more than one minus sign in a string, you don't need a regular
expression, you can use the string.split method:

Dim bMoreThanOne as Boolean

bMoreThanOne = (MyString.Split("-"c).Count > 2)

I'm not sure if this will perform any faster than a regular expression.
Usign an regular Expression you can do this:

Dim s As String = "ssdfsdf-sdfsdfsdfsdf-sdfsdfsdf-sdfsdf"

Dim rx As New Regex("-")
bMoreThanOne = (rx.Matches(s).Count > 1)

This should work if the string contains either - or + but I have not
tested it

Dim rx As New Regex(".*-.*-.*|.*\+.*\+.*")

Hope this helps


Nov 21 '05 #3
vbmark,
In addition to the other comments.

You can use a single regex to check for a string with either 2 "-" or 2 "+",
do you expect them to be consecutive or is stuff allowed to between them...

Here is a pattern that allows anything between the "-" or "+".

Const pattern as String = "\-.*\-|\+.*\+"

(Note its a simpler version of Chris', the leading & trailing ".*" are
implied).
Expresso & RegEx Workbench both have wizards of varying degrees to help you
build your expression, plus they allow you to test your expressions, also
the analyzer/interpreter in each is rather handy.

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/U...-4ee2729d7322A

tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...geElements.asp

Hope this helps
Jay
"vbmark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
I'm new to RegEx in vb.net so I'm not sure how to do this.

I want to know if a string contains two minus signs "-". If there are two
then I want it to return TRUE.

I also need to know if the string contains two plus signs "+". Should
this
be a seperate RegEx or can one RegEx check for both signs?

Thanks!

Nov 21 '05 #4
They may or may not be consecutive.

What I want is to only allow the user to have:

ONE minus sign *OR* ONE plus sign

in my text box.

So I modified it to this:

Const pattern As String = "\-.*\-|\+.*\+|\-.*\+|\+.*\-"
Dim rx As New Regex(pattern)
If rx.Matches(thisTextBox.Text).Count > 1 Then
e.Handled = True
End If

But it allows me to do this

2-2-

So it's not working.

Changing to this

Dim rx As New Regex("-")

Works. But of course only for the minus sign.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
news:en**************@TK2MSFTNGP12.phx.gbl:
vbmark,
In addition to the other comments.

You can use a single regex to check for a string with either 2 "-" or
2 "+", do you expect them to be consecutive or is stuff allowed to
between them...

Here is a pattern that allows anything between the "-" or "+".

Const pattern as String = "\-.*\-|\+.*\+"

(Note its a simpler version of Chris', the leading & trailing ".*" are
implied).
Expresso & RegEx Workbench both have wizards of varying degrees to
help you build your expression, plus they allow you to test your
expressions, also the analyzer/interpreter in each is rather handy.

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/U...px?SampleGuid=
c712f2df-b026-4d58-8961-4ee2729d7322A

tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...y/en-us/cpgenr
ef/html/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
"vbmark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
I'm new to RegEx in vb.net so I'm not sure how to do this.

I want to know if a string contains two minus signs "-". If there
are two then I want it to return TRUE.

I also need to know if the string contains two plus signs "+".
Should this
be a seperate RegEx or can one RegEx check for both signs?

Thanks!



Nov 21 '05 #5
vbmark <no@email.com> wrote in
news:Xn************************@130.133.1.4:
I'm new to RegEx in vb.net so I'm not sure how to do this.

I want to know if a string contains two minus signs "-". If there are
two then I want it to return TRUE.

I also need to know if the string contains two plus signs "+". Should
this be a seperate RegEx or can one RegEx check for both signs?

Thanks!


Thanks to Expresso <http://www.ultrapico.com/Expresso.htm>

The answer is "[-+].*[-+]"

Thanks!

Nov 21 '05 #6
vbmark,
I want to know if a string contains two minus signs "-". If there
are two then I want it to return TRUE.
I also need to know if the string contains two plus signs "+".
What I want is to only allow the user to have:
ONE minus sign *OR* ONE plus sign Doh! changing the requirements on us. Typical User! :-)

What is it specifically you want?

If you want 2 "-", 2 "+" or "+" & "-"

You can simplify it to:
Const pattern As String = "[\-\+].*[\-\+]"
If rx.Matches(thisTextBox.Text).Count > 1 Then Wouldn't you want > 0? as you don't want any matches to be found.

I would probably simply use:
If Not rx.IsMatch(thisTextBox.Text) Then
Which says I don't want any matches..

Dim rx As New Regex("\-|\+") and Dim rx As New Regex("[\-\+]")
Will match a "+" or a "-".

Again review the links I gave earlier as they explain HOW regular
expressions work. The two utilities I gave show HOW regular expressions
work. For example, enter "[\-\+].*[\-\+]" or "\-.*\-|\+.*\+|\-.*\+|\+.*\-"
in the regular expression tab of Expresso, then select the Analyzer tab in
Expresso, it will explain to you what the expression is doing.
I guessing that you really want to ensure that the string has a single "+"
or a single "-". You could use "[\-\+]" and check for number of matches...
Or possibly this one "^[^\-\+]*[\-\+][^\-\+]*$" and IsMatch.

Hope this helps
Jay
"vbmark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4... They may or may not be consecutive.

What I want is to only allow the user to have:

ONE minus sign *OR* ONE plus sign

in my text box.

So I modified it to this:

Const pattern As String = "\-.*\-|\+.*\+|\-.*\+|\+.*\-"
Dim rx As New Regex(pattern)
If rx.Matches(thisTextBox.Text).Count > 1 Then
e.Handled = True
End If

But it allows me to do this

2-2-

So it's not working.

Changing to this

Dim rx As New Regex("-")

Works. But of course only for the minus sign.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
news:en**************@TK2MSFTNGP12.phx.gbl:
vbmark,
In addition to the other comments.

You can use a single regex to check for a string with either 2 "-" or
2 "+", do you expect them to be consecutive or is stuff allowed to
between them...

Here is a pattern that allows anything between the "-" or "+".

Const pattern as String = "\-.*\-|\+.*\+"

(Note its a simpler version of Chris', the leading & trailing ".*" are
implied).
Expresso & RegEx Workbench both have wizards of varying degrees to
help you build your expression, plus they allow you to test your
expressions, also the analyzer/interpreter in each is rather handy.

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/U...px?SampleGuid=
c712f2df-b026-4d58-8961-4ee2729d7322A

tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/de...y/en-us/cpgenr
ef/html/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
"vbmark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
I'm new to RegEx in vb.net so I'm not sure how to do this.

I want to know if a string contains two minus signs "-". If there
are two then I want it to return TRUE.

I also need to know if the string contains two plus signs "+".
Should this
be a seperate RegEx or can one RegEx check for both signs?

Thanks!


Nov 21 '05 #7
Doesn't the + sign need to be escaped since it is used as a RegEx meta
character in .Net?

Nov 21 '05 #8
Chris,
The square brackets "escape" the +, so in this instance its not needed.

Normally the - means a range in square brackets, however seeing as the - is
the first character it is taken 'literally'...

Although in my examples I used \+ & \- just to be certain...

IMHO This is where Expresso or RegEx Workbench identified in my earlier post
are useful, they allow you to quickly test the expressions with needing to
write a program to do it...

Hope this helps
Jay

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Doesn't the + sign need to be escaped since it is used as a RegEx meta
character in .Net?

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by engwar1@yahoo.com | last post: by
4 posts views Thread by aevans1108@yahoo.com | last post: by
2 posts views Thread by Tim Conner | last post: by
6 posts views Thread by Du Dang | last post: by
17 posts views Thread by clintonG | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Martin Evans | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.