Extract text in brackets using regular expressions

In sentences like:

"[x] Alpha

[33] Beta

I retrieve the data array in brackets as ([x], [33])

using the VBA regular expression pattern:

"(\[x\])|(\[\d*\])"

I cannot directly retrieve an array of data without parentheses like (x, 33)

using web resource recommendations for a template

"(?<=\[)(.*?)(?=\])"

Is this a specific VBA problem (that is, restricting the implementation of Regex) or did I misunderstand the "look back and forth"?

Public Function Regx( _
  ByVal SourceString As String, _
  ByVal Pattern As String, _
  Optional ByVal IgnoreCase As Boolean = True, _
  Optional ByVal MultiLine As Boolean = True, _
  Optional ByVal MatchGlobal As Boolean = True) _
  As Variant

Dim oMatch As Match
Dim arrMatches
Dim lngCount As Long

' Initialize to an empty array
arrMatches = Array()
With New RegExp
    .MultiLine = MultiLine
    .IgnoreCase = IgnoreCase
    .Global = MatchGlobal
    .Pattern = Pattern
    For Each oMatch In .Execute(SourceString)
        ReDim Preserve arrMatches(lngCount)
        arrMatches(lngCount) = oMatch.Value
        lngCount = lngCount + 1
    Next
End With


Sub testabove()
    Call Regx("[x] Alpha" & Chr(13) & _
      "[33] Beta", "(\[x\])|(\[\d*\])")
End Sub
+4
source share
4 answers

Use the grab around the subpatterns to get the value you need.

Use

"\[(x)\]|\[(\d*)\]"

( \d+, , * , + ).

- :

"\[([^\][]+)]"

Submatches, ( , ), . for

For Each oMatch In .Execute(SourceString)
    ReDim Preserve arrMatches(lngCount)
    If Len(oMatch.SubMatches(0)) > 0 Then
        arrMatches(lngCount) = oMatch.SubMatches(0)
    Else
        arrMatches(lngCount) = oMatch.SubMatches(1)
    End If
    ' Debug.Print arrMatches(lngCount) ' - This outputs x and 33 with your data
    lngCount = lngCount + 1
Next
+3

:

\[(x)\]|\[(\d*)\]

, , ().

You will get x and 33 in $1 and $2

Dot Net

, , vb . ,

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim text As String = "[x] Alpha      [33] Beta]"
      Dim pattern As String = "\[(x)\]|\[(\d*)\]"

      ' Instantiate the regular expression object.
      Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)

      ' Match the regular expression pattern against a text string.
      Dim m As Match = r.Match(text)
      Dim matchcount as Integer = 0
      Do While m.Success
         matchCount += 1
         Console.WriteLine("Match" & (matchCount))
         Dim i As Integer
         For i = 1 to 2
            Dim g as Group = m.Groups(i)
            Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
            Dim cc As CaptureCollection = g.Captures
            Dim j As Integer 
            For j = 0 to cc.Count - 1
              Dim c As Capture = cc(j)
               Console.WriteLine("Capture" & j & "='" & c.ToString() _
                  & "', Position=" & c.Index)
            Next 
         Next 
         m = m.NextMatch()
      Loop
   End Sub
End Module
+1

In Excel and VBA, you can cut brackets after extracting regular expressions:

Sub qwerty()

    Dim inpt As String, outpt As String
    Dim MColl As MatchCollection, temp2 As String
    Dim regex As RegExp, L As Long

    inpt = "38c6v5hrk[x]537fhvvb"

    Set regex = New RegExp
    regex.Pattern = "(\[x\])|(\[\d*\])"
    Set MColl = regex.Execute(inpt)
    temp2 = MColl(0).Value

    L = Len(temp2) - 2
    outpt = Mid(temp2, 2, L)

    MsgBox inpt & vbCrLf & outpt
End Sub

enter image description here

+1
source

Array without regex:

For Each Value In Split(SourceString, Chr(13))
  ReDim Preserve arrMatches(lngCount)
  arrMatches(lngCount) = Split(Split(Value, "]")(0), "[")(1)
  lngCount = lngCount + 1
Next
+1
source

Source: https://habr.com/ru/post/1659195/


All Articles