Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for a special string pattern on multiple levels

I need to evaluate a string pattern that is something like as given below and I am quite new at writing such complex expressions

<%(ddlValue){DropDownList}[SelectedValue]%>
// this has three part (Control Name) {Control Type} [Control Property]

I tried a whole lot of regex and other tools like RegExr but anything did not worked. I have to do this on four levels, that is as given below in the code. So here is what I have done:

string regex = "/[<%](.*?)[%>]/g"; // Regex to match "<% %>" pattern
Match mtch = Regex.Match(strQuery, regex, RegexOptions.Singleline);
    string strControlName = "";
    string strControlType = "";
    string strControlProp = "";
    if (mtch.Success)
    {
        string strVal = mtch.Value;
        Match mtchControlName = Regex.Match(strVal, "/[(]\S)/"); 
        // Regex to match "()" i.e. control name ("ddlValue" in above example)
        if (mtchControlName.Success)//Match control Name
        {
            strControlName = mtchControlName.Value;
            Match mtchControlType = Regex.Match(strVal, "/[{]\s[}]/"); 
            // Regex to match "[]" i.e. control type
            if (mtchControlType.Success) // Match Control Type
            {
                strControlType = mtchControlType.Value;
                Match mtchControlProp = Regex.Match(strVal, "/[(]\S[)]/"); 
                // Regex to match "[]" i.e. control property
                if (mtchControlProp.Success) // Match Control Prop
                {
                     strControlProp = mtchControlProp.Value;
                }
             }
         }
     }
like image 737
Cyberpks Avatar asked Sep 04 '26 11:09

Cyberpks


2 Answers

You can do this in a single regex. Being as specific as possible, you could do this:

Regex regexObj = new Regex(
    @"\(           # Match (
    (              # Capture in group 1:
     [^()]*        # Any number of characters except ()s
    )              # End of group 1
    \)             # Match )
    \{([^{}]*)\}   # The same for {...} into group 2
    \[([^\[\]]*)\] # The same for [...] into group 3", 
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

Then you case use

Match matchResults = regexObj.Match(subjectString);

to get a Match object. Access the submatches via

matchResults.Groups(n).Value   // insert 1, 2 or 3 for n

See it live on regex101.com.

like image 84
Tim Pietzcker Avatar answered Sep 06 '26 00:09

Tim Pietzcker


You can use capturing groups in one expression to capture all groups together:

String input = "<%(ddlValue){DropDownList}[SelectedValue]%>";
String pattern = @"<%\((.+)\)\{(.+)\}\[(.+)\]%>";

Match m = Regex.Match(input, pattern);

if (m.Groups.Count == 4) 
{
    string firstpart = m.Groups[1].ToString();
    string secondpart = m.Groups[2].ToString();
    string thirdpart = m.Groups[3].ToString();
}
like image 32
Szymon Avatar answered Sep 06 '26 01:09

Szymon