473,542 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1813
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.goo glegroups.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(this TextBox.Text).C ount > 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******** ******@TK2MSFTN GP12.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/cpconRegularExp ressionsLanguag eElements.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.c om/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(this TextBox.Text).C ount > 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(this TextBox.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(this TextBox.Text).C ount > 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******** ******@TK2MSFTN GP12.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/cpconRegularExp ressionsLanguag eElements.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.goo glegroups.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
4354
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 people are entering as their new password. Must be between 6 and 10 characters Must be alphanumeric only Can not be the word "password" in any...
4
9709
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 numeric value according to an arbitrary regular expression.
2
3412
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 was splitted. I am fascinated to the powerfull use of this RegEx class, so I wonder if it could go a step further. As a question, can regex be...
6
396
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
3945
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 http://forta.com/books/0672325667/
5
5078
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated by a tool. ' Runtime Version: 1.1.4322.2032 ' ' Changes to this file may cause incorrect behavior and will be lost if ' the code is...
6
5863
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". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
7
2568
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 slash then some numbers. For some reason I am not getting that. It won't work at all in 2.0
6
4194
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 but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most...
6
2057
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 it is called a 100 times but in my real app it could be called 50000 or more times. So my question is if it is somehow possible to speed this up...
0
7397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7582
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7675
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5257
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3384
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.