Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 614627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:07:55+00:00 2026-05-13T18:07:55+00:00

I had a regex, like so: (?<one-1>cat)|(?<two-2>dog)|(?<three-3>mouse)|(?<four-4>fish) When I tried to use this pattern

  • 0

I had a regex, like so:

(?<one-1>cat)|(?<two-2>dog)|(?<three-3>mouse)|(?<four-4>fish)

When I tried to use this pattern in a .Net app, it failed, because the group name contained a ‘-‘ in it.

So, as a workaround, I tried to use two regexes, the first:

(?<A>cat)|(?<Be>dog)|(?<C>mouse)|(?<D>fish)

would match the original cases I was looking for into group names I could control.
And then, I intended to use the correctly matched group name from that regex in one like this:

(?<A>one-1)|(?<Be>two-2)|(?<C>three-3)|(?<D>four-4)

I would do so, by finding the string that matched this pattern and determining if the group names were equal.

I know this seems a bit convoluted. Thanks of any help offered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:07:55+00:00Added an answer on May 13, 2026 at 6:07 pm

    Something along the lines of the following?

    string[,] patterns = {
        { "one-1", "cat" },
        { "two-2", "dog" },
        { "three-3", "mouse" },
        { "four-4", "fish" },
    };
    
    var regex = buildRegex(patterns);
    
    string[] tests = { "foo", "dog", "bar", "fish" };
    foreach (var t in tests) {
        var m = regex.Match(t);
        Console.WriteLine("{0}: {1}", t, reportMatch(regex, m));
    }
    

    Output

    foo: no match
    dog: two-2 = dog
    bar: no match
    fish: four-4 = fish

    First we build up a Regex instance by escaping the group names and combining them with the patterns. Any non-word character is replaced with the sequence _nnn_ where nnn is its UTF-32 value.

    private static Regex buildRegex(string[,] inputs)
    {   
        string regex = ""; 
        for (int i = 0; i <= inputs.GetUpperBound(0); i++) {
            var part = String.Format(
                "(?<{0}>{1})",
                Regex.Replace(inputs[i,0], @"([\W_])", new MatchEvaluator(escape)),
                inputs[i,1]);
    
            regex += (regex.Length != 0 ? "|" : "") + part;
        }   
    
        return new Regex(regex);
    }   
    
    private static string escape(Match m)
    {
        return "_" + Char.ConvertToUtf32(m.Groups[1].Value, 0) + "_";
    }   
    

    For matches, the .NET library doesn’t give us an easy way to get a group’s name, so we have to go the other way: for each group name, we check whether that group matched and if so unescape its name and let the caller know both name and captured substring.

    private static string reportMatch(Regex regex, Match m)
    {   
        if (!m.Success)
            return "no match";
    
        foreach (var name in regex.GetGroupNames()) {
            if (name != "0" && m.Groups[name].Value.Length > 0)
                return String.Format(
                           "{0} = {1}",
                           Regex.Replace(name, @"_(\d+)_",
                               new MatchEvaluator(unescape)),
                           m.Groups[name].Value);
        }
    
        return null;
    }   
    
    private static string unescape(Match m)
    {   
        return Char.ConvertFromUtf32(int.Parse(m.Groups[1].Value));
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it’s been a while since the last time I had to use regex, I
Had this working; at one stage. The problem is the following text is now
I would like to use regex to look inside a string for the smallest
I had a quick question about Django URL configuration, and I guess REGEX as
I'm facing a problem with Regex... I had to match sharepoint URL.. I need
Had a good read through similar topics but I can't quite a) find one
I can't get my head around a solid RegEx for doing this, still very
I had a quick question regarding RegEx... I have a string that looks something
Tried posting this before but it did not go through (i think) so if
What's your thought - what does a Regex look like, that will never be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.