[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

RegexOptions

Posted on 2008-11-04
3
Medium Priority
?
675 Views
Last Modified: 2013-12-17
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
Comment
Question by:ddrudik
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22881405
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
 
LVL 27

Author Comment

by:ddrudik
ID: 22881713
Perfect, thanks.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22882689
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question