• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

RegexOptions

I have regex object:
Regex reOperation = new Regex(matchpattern);

I also have a string:
regoptions = "RegexOptions.IgnoreCase | RegexOptions.Multiline";

I would like to define the object as:
Regex reOperation = new Regex(matchpattern,regoptions);

Or in some similar fashion where regoptions is a dynamic string containing the desired RegexOptions.
0
ddrudik
Asked:
ddrudik
  • 2
1 Solution
 
Fernando SotoRetiredCommented:
Hi ddrudik;

You cannot directly assign a string with RegexOptions to a Regex object because it is looking for an enumeration Or'ed together. The code snippet below will do that for you.

Fernando

private void button1_Click(object sender, EventArgs e)
{
    String matchpattern = @"^\w{10}";
    RegexOptions regoptions = GetRegexOptions("RegexOptions.IgnoreCase | RegexOptions.Multiline");
    Regex reOperation = new Regex(matchpattern, regoptions);
}
 
private RegexOptions GetRegexOptions(String opt)
{
    RegexOptions retOpt = RegexOptions.None;
    String[] strOpt = opt.Split(new String[] {"|"}, StringSplitOptions.RemoveEmptyEntries);
    foreach (String regexOpt in strOpt)
    {
        switch (regexOpt.Trim())
        {
            case "RegexOptions.Compiled":
                retOpt |= RegexOptions.Compiled;
                break;
            case "RegexOptions.CultureInvariant":
                retOpt |= RegexOptions.CultureInvariant;
                break;
            case "RegexOptions.ECMAScript":
                retOpt |= RegexOptions.ECMAScript;
                break;
            case "RegexOptions.ExplicitCapture":
                retOpt |= RegexOptions.ExplicitCapture;
                break;
            case "RegexOptions.IgnoreCase":
                retOpt |= RegexOptions.IgnoreCase;
                break;
            case "RegexOptions.IgnorePatternWhitespace":
                retOpt |= RegexOptions.IgnorePatternWhitespace;
                break;
            case "RegexOptions.Multiline":
                retOpt |= RegexOptions.Multiline;
                break;
            case "RegexOptions.RightToLeft":
                retOpt |= RegexOptions.RightToLeft;
                break;
            case "RegexOptions.Singleline":
                retOpt |= RegexOptions.Singleline;
                break;
            default:
                break;
        }
    }
 
    return retOpt;
}

Open in new window

0
 
ddrudikAuthor Commented:
Perfect, thanks.
0
 
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.  ;=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now