Need RegEx help. How can I grab anything between a set of apostrophes?

simpleonline

Active member
Joined
Sep 13, 2011
Messages
33
Programming Experience
Beginner
My app needs to Parse the text to grab only the numbers, letters inside the apstrophes as seen below.

VB.NET:
challenge : '03AHJ_VusHZDEr-qZYnfmRm4QbevEdYMXPLUibIP-bInEgiAPc0tk4e5MAVntRv_JbvD0q51aJOQp1FZ8RmIaMGt-Te9CSwkZyJhvSK70ZvdOA0AhPJz76t1FhXekXWIuor6YqdVwbqXSFlUSQvi9eL455ojSBkoQW_w'

My current string for this is the same as above but my regex to get only the information between these apstrophnes doesn't return a results.

Note that I am trying to get all letters and numbers [a-zA-Z0-9]. My main issue is that the code has random - and _ codes in the challenge.

How can I configure my Regex to just grab anything inside the apstrophes?

thanks

VB.NET:
Sub RegExGet1()

        Dim value As String = "'challenge : '03AHJ_VusHZDEr-qZYnfmRm4QbevEdYMXPLUibIP-bInEgiAPc0tk4e5MAVntRv_JbvD0q51aJOQp1FZ8RmIaMGt-Te9CSwkZyJhvSK70ZvdOA0AhPJz76t1FhXekXWIuor6YqdVwbqXSFlUSQvi9eL455ojSBkoQW_w'"
         Dim m As Match = Regex.Match(ResponseIT.Text, "'challenge : '[a-zA-Z0-9]'", RegexOptions.IgnoreCase)
        If (m.Success) Then
            Dim key As String = m.Groups(1).Value
            KeyIt.Text = (key)
       
        End If
    End Sub
 
I would just not use RegEx personally....

key = value.Substring(value.IndexOf(": '") + 3, value.LastIndexOf("'") - value.IndexOf(": '") - 3)
 
Last edited:
While i don't want to go into the debate about parsing HTML using regular expressions. It's an option, One that i do use a lot.

Imports System.Text.RegularExpressions

Public Class MainForm
    Private Sub MatchHtml()
        Dim value As String = "'challenge :   '03AHJ_VusHZDEr-qZYnfmRm4QbevEdYMXPLUibIP-bInEgiAPc0tk4e5MAVntRv_JbvD0q51aJOQp1FZ8RmIaMGt-Te9CSwkZyJhvSK70ZvdOA0AhPJz76t1FhXekXWIuor6YqdVwbqXSFlUSQvi9eL455ojSBkoQW_w'"
        Dim pattern As String = "(?<='challenge : ').+(?=')"
        Dim regex As New Regex(pattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
        Dim match As Match = regex.Match(value)

        If match.Success Then
            MessageBox.Show(String.Format("Challenge : {0}{0}{1}", Environment.NewLine, match.Value))
        End If
    End Sub
End Class

 
Dim pattern As String = "(?<='challenge : ').+(?=')"
You can also do the opposite and get the group value, it is sometimes more efficient and also easier to code than lookarounds:
        Dim input As String = "'challenge : '03AHJ_VusHZDEr-qZYnfmRm4QbevEdYMXPLUibIP-bInEgiAPc0tk4e5MAVntRv_JbvD0q51aJOQp1FZ8RmIaMGt-Te9CSwkZyJhvSK70ZvdOA0AhPJz76t1FhXekXWIuor6YqdVwbqXSFlUSQvi9eL455ojSBkoQW_w'"
        Dim pattern As String = "challenge : '(.+)'"
        Dim m As Match = Regex.Match(input, pattern)
        Dim value = m.Groups(1).Value
 
Back
Top