RegEx question

V

vbmark

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!
 
C

Chris Dunaway

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
 
V

vbmark

Wow! Either of those will work fine. They don't have to be next to each
other.

Thanks!
 
J

Jay B. Harlow [MVP - Outlook]

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/...pleGuid=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/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 
V

vbmark

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.
 
V

vbmark

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!
 
J

Jay B. Harlow [MVP - Outlook]

vbmark,
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
 
C

Chris Dunaway

Doesn't the + sign need to be escaped since it is used as a RegEx meta
character in .Net?
 
J

Jay B. Harlow [MVP - Outlook]

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
 

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

Top