472,364 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 1718
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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.