1
[15-]
[41-(32)] 
[48-(45)] 
[70-15] 
[40-(64)] 
[(128)-42] 
[(128)-56] 

I have these values for which I want to extract the value not in curled brackets. If there is more than one, then add them together.

What is the regular expression to do this?

So the solution would look like this:

[15-] -> 15
[41-(32)] -> 41
[48-(45)] -> 48
[70-15] -> 85  
[40-(64)] -> 40
[(128)-42] -> 42
[(128)-56] -> 56
JasonJ
  • 46
  • 4
  • this must be your assignment .... and what you have searched/tried so far Show your effort – Usman Kurd Aug 28 '13 at 05:33
  • Fair enough. My values will not be less than 10 or greater than 196, so this is what I have so far: \b([1-9][0-9]|1[0-9][0-9]|)\b. It's part of an Excel VBA function which returns the matchs as a MatchCollection, which I then add together. Next I'm just tyring to work out how to exclude the numbers in curled brackets. – JasonJ Aug 28 '13 at 06:01

4 Answers4

0

Regular expressions does not support performing math on the terms. You can loop through the groups that are matched and perform the math outside of Regex.

Here's the pattern to extract any number within the square brackets that are not in cury brackets:

\[
  (?:(?:\d+|\([^\)]*\))-)*
  (\d+)
  (?:-[^\]]*)*
\]

Each number will be returned in $1.

This works by looking for a number that is prefixed by any number of "words" separated by dashes, where the "words" are either numbers themselves or parenthesized strings, and followed by, optionally, a dash and some other stuff before hitting the end brace.

If VBA's RegEx doesn't support uncaptured groups (?:), remove all of the ?:'s and your captured numbers will be in $3 instead.

A simpler pattern also works:

\[
  (?:[^\]]*-)*
  (\d+)
  (?:-[^\]]*)*
\]

This simply looks for numbers delimited by dashes and allowing for the number to be at the beginning or end.

richardtallent
  • 34,724
  • 14
  • 83
  • 123
  • I'm using Excel with VBA and a reference to Microsoft VBScript Regular Expressions 5.5. Will the pattern still be the same? – JasonJ Aug 28 '13 at 07:18
  • Yes, I believe so. Just remember to remove the whitespace. More info on VBScript's RegEx support: http://www.regular-expressions.info/vbscript.html – richardtallent Aug 29 '13 at 07:05
0

You would be over complicating if you go for a regex approach (in this case, at least), also, regular expressions does not support mathematical operations, as pointed out by @richardtallent.

You can use an approach as shown here to extract a substring which omits the initial and final square brackets, and then, use the Split (as shown here) and split the string in two using the dash sign. Lastly, use the Instr function (as shown here) to see if any of the substrings that the split yielded contains a bracket.

If any of the substrings contain a bracket, then, they are omitted from the addition, or they are added up if otherwise.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
0
Private Sub regEx()

    Dim RegexObj As New VBScript_RegExp_55.RegExp
    RegexObj.Pattern = "\[(\(?[0-9]*?\)?)-(\(?[0-9]*?\)?)\]"

    Dim str As String
    str = "[15-]"

    Dim Match As Object
    Set Match = RegexObj.Execute(str)



    Dim result As Integer
    Dim value1 As Integer
    Dim value2 As Integer

    If Not InStr(1, Match.Item(0).submatches.Item(0), "(", 1) Then
        value1 = Match.Item(0).submatches.Item(0)
    End If

    If Not InStr(1, Match.Item(0).submatches.Item(1), "(", 1) And Not Match.Item(0).submatches.Item(1) = "" Then
        value2 = Match.Item(0).submatches.Item(1)
    End If

    result = value1 + value2

    MsgBox (result)
End Sub

Fill [15-] with the other strings.

Uriel Katz
  • 319
  • 1
  • 8
  • 21
-2

Ok! It's been 6 years and 6 months since the question was posted. Still, for anyone looking for something like that maybe now or in the future...

Step 1: Trim Leading and Trailing Spaces, if any

Step 2: Find/Search:

\]|\[|\(.*\)

Replace With:

<Leave this field Empty>

Step 3: Trim Leading and Trailing Spaces, if any

Step 4: Find/Search:

^-|-$

Replace With:

<Leave this field Empty>

Step 5: Find/Search:

-

Replace With:

\+
Community
  • 1
  • 1
  • Welcome to Stack Overflow! While this might be a valuable hint to solve the problem, a good answer also *demonstrates* the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Feb 26 '20 at 17:57