-1

Please suggest me regex pattern for the string like below: <[XYZ-ABC]>

I have to find these strings using Microsoft.Office.Interop.Word and then save the search string in database.

Thanks for any help provided.

Ruchi
  • 1,238
  • 11
  • 32

2 Answers2

2

Got one answer please let me know if its correct:

<[^/>]*>
Anirudha
  • 32,393
  • 7
  • 68
  • 89
Ruchi
  • 1,238
  • 11
  • 32
0

Try this:

private Regex angleReg = new Regex(@"<([^>]+)>\s+<([^>]+)>");

private string[] parse(string rawInput)
{
    Match angleMatch = angleReg.Match(rawInput);

    if (angleMatch.Success)
    {
        return new string[] { angleMatch.Groups[1].Value, angleMatch.Groups[2].Value };
    }
    else
    {
        return null;
    }
}

In a .NET regex, the things like [^abcd] mean "anything not a, b, c or d" so in our case we want anything not ">". [^>]+ means "anything not >" "one or more times" which is what + is. So a+ matches "a", "aa", "aaa", etc. (x)(y) matches "xy" but then, in your Match object, the .Groups list will contain "x" at Groups[1] and "y" at Groups[2] for easy access to the matched strings.

JDB
  • 25,172
  • 5
  • 72
  • 123
welegan
  • 3,013
  • 3
  • 15
  • 20