473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
8 1802
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.