Regexp help, I need a regex that accept a string of (+) or (-) followed by a number

danitouffaha

New Member
Joined
Nov 16, 2012
Messages
18
I want to write a regular expression in vba that accepts 1,2 or 3 (-) or 1,2 or 3 (+) and series of digits(0-9), and it can repeat
so for example +++5+2-10+6 is accepted
++++5 is not accepted because there 4 (+) as well as ----10+2 not accepted because there are 4 (-)
I have tried many regex that did not work
VBA Code:
Sub Test_Pattern()
Dim Str As String
Dim regexObject As RegExp
Set regexObject = New RegExp
With regexObject
   'checks if any word in our string start with an alphabet between W and Z
   .pattern = "[\+|\+\+|\+\+\+][0-9]{1,}"
End With
Str = "++++55"
'A message box displays true because we have a word starting with W ‘World’
MsgBox regexObject.test(Str)
End Sub

I think it should be easy but couldnt find anything to help me resolve it

Thanks
 
Last edited by a moderator:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

If the acceptable strings consist of nothing other than 1 to 3 + signs or 1 to 3 - signs followed by 1 or more digits (all of that possibly repeated) then try this pattern line.
VBA Code:
.Pattern = "^((\+{1,3}|\-{1,3})\d+)+$"
 
Upvote 0
Solution
You're welcome. Thanks for the follow-up, :)
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top