none
REGEX to find words in brackets

    Question

  • Hi All,

    I'm using a stream reader to read through a text file and I would like to use Regex to find any word encapsulated in brackets. as I understand it, Brackets are metadata and cannot be used this way.

    isnt there some sort of escape method that will allow me to do this?

    basically I  want regex to return a success if it finds any word inside of brackets like this: [hello world]

    can anyone help me out?

    thank you

    Chris

     

    Wednesday, October 09, 2013 3:46 PM

Answers

  • Try this code

    Imports System.Text.RegularExpressions
    Module Module1
        Sub Main()
            Dim input() As String = { _
                "line 1 [abc]\n", _
                "line 2 [def]\n", _
                "line 3 [ghi]\n", _
                "line 4 [jkl]\n"}
            Dim pattern As String = "\[(?'Word'.*)\]"
            Dim exp As Regex = New Regex(pattern)
            For Each inputLine As String In input
                Dim nMatch As Match = exp.Match(inputLine)
                For Each capt As Capture In nMatch.Groups("Word").Captures
                    Console.WriteLine("The word is : {0}", capt.Value)
                Next capt
            Next inputLine
        End Sub
    End Module


    jdweng

    Wednesday, October 09, 2013 4:51 PM

All replies

  • Try this code

    Imports System.Text.RegularExpressions
    Module Module1
        Sub Main()
            Dim input() As String = { _
                "line 1 [abc]\n", _
                "line 2 [def]\n", _
                "line 3 [ghi]\n", _
                "line 4 [jkl]\n"}
            Dim pattern As String = "\[(?'Word'.*)\]"
            Dim exp As Regex = New Regex(pattern)
            For Each inputLine As String In input
                Dim nMatch As Match = exp.Match(inputLine)
                For Each capt As Capture In nMatch.Groups("Word").Captures
                    Console.WriteLine("The word is : {0}", capt.Value)
                Next capt
            Next inputLine
        End Sub
    End Module


    jdweng

    Wednesday, October 09, 2013 4:51 PM
  • Add "\" before any of the special characters i.e . * / \ ] [ ( ) { }

    Read more about Regular Expressions here: http://vimregex.com/


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, October 09, 2013 5:06 PM
  • Sweet!

    This looks like it will work great! Thank you..

    Chris

    Wednesday, October 09, 2013 5:20 PM
  • Sweet!

    This looks like it will work great! Thank you..

    Chris

    Sorry I jumped the gun.. that won't actually work.

    The . will include the ] so it will catch everything after [.

    You'll need to make an explicit group of characters like "a-zA-Z0-9 " instead of the just "." and you may have to add other characters as well depending on what might actually appear in the brackets.

    And then it also only gets one match so if there can be more than one word in brackets per string you need to keep matching.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Wednesday, October 09, 2013 5:25 PM
    Moderator
  • Maybe something like:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim pattern As String = "(?:\[)([a-zA-Z0-9 ]*)(?:\])"
            Dim text As String = "line 1 [abc]\nline 2 [def]\nline 3 [ghi]\nline 4 [jkl]\n"
    
            Dim match As System.Text.RegularExpressions.Match
            match = System.Text.RegularExpressions.Regex.Match(text, pattern)
    
            While match.Success
                Debug.Print(match.Groups(1).Captures(0).Value)
                match = match.NextMatch
            End While
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, October 09, 2013 5:37 PM
    Moderator
  • I noticed you had spaces in the blank lines.  Try this new pattern

                string pattern = @"(.*[^\n]\s*[^\n].*\n){8}";


    jdweng

    Wednesday, October 09, 2013 5:51 PM
  • After Playing around with your code, I got it working but dont quite understand it all...

    here is how im using it:

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim MyFilePath As New FileStream("C:\test\GrandChild.ini", FileMode.Open, FileAccess.Read)
            Dim MyStreamReader As New StreamReader(MyFilePath)
            Dim Line As String 'contents of each line read by the StreamReader
          
    
            Do While Not MyStreamReader.EndOfStream
                Line = MyStreamReader.ReadLine
    
                'declarations for REGEX
                Dim Source As String = Line
                Dim Pattern As String = "\[(?'X'.*)\]"
                Dim Result As Match = Regex.Match(Source, Pattern)
                Dim Found As String = Result.Groups(1).Value
                    MsgBox("found " & Found)

    so my question is...what is the X used for? ('X')

    I think it might be the name of the group where the result is stored but that should be covered by the variable Found

    any thoughts?

    thank you.

    Chris

    Wednesday, October 09, 2013 6:26 PM
  • the X just gives a name to the capture group.  I used "Word" as he capture name.

    jdweng

    Wednesday, October 09, 2013 6:40 PM
  • This pattern works also "(\[.*?\])" for getting characters enclosed between open and close brackets.

    Option Strict On
    
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Dim CheckData As String = ""
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            CheckData = My.Computer.FileSystem.ReadAllText("C:\Users\John\Desktop\Testing.Txt")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim SearchPattern As String = "(\[.*?\])"
            Dim SearchReturn = Regex.Matches(CheckData, SearchPattern)
            Label1.Text = "The search returned " & SearchReturn.Count.ToString & " results."
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RichTextBox1.Text = ""
            Dim SearchPattern As String = "(\[.*?\])"
            Dim SearchReturn = Regex.Matches(CheckData, SearchPattern)
            If SearchReturn.Count > 0 Then
                For Each Item In SearchReturn
                    RichTextBox1.AppendText(Item.ToString & vbCrLf)
                Next
            End If
        End Sub
    End Class
    


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Thursday, October 10, 2013 8:47 AM
  • This pattern works also "(\[.*?\])" for getting characters enclosed between open and close brackets.

    Good catch Monkeyboy.  Regex always messes with me... so many options.

    Anyway, the question mark before the "\]" works perfectly for eliminating that character from the previous ".*" match, and we can still use non-capture groups for the brackets so that we only get the inner word back:

     "(?:\[)(.*?)(?:\])"

    So now the same example as before, with the simpler pattern, yields the same results:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim pattern As String = "(?:\[)(.*?)(?:\])"
            Dim text As String = "line 1 [abc] fafa\nline 2 [def]\nline 3 [ghi]\nline 4 [jkl]\n"
    
            Dim match As System.Text.RegularExpressions.Match
            match = System.Text.RegularExpressions.Regex.Match(text, pattern)
    
            While match.Success
                Debug.Print(match.Groups(1).Captures(0).Value)
                match = match.NextMatch
            End While
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, October 10, 2013 2:51 PM
    Moderator