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

  • SEARCH
  • Home
  • 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 3931570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:21:51+00:00 2026-05-19T23:21:51+00:00

I’m a bit lost creating a RegEx under C#.NET. I’m doing something like parser,

  • 0

I’m a bit lost creating a RegEx under C#.NET.
I’m doing something like parser, so I use Regex.Replace to search text for certain “variables” and replace them with their “values”.
Each variable starts with ampersand (“&”) and ends with ampersand (begining of another variable) or dot.
Each variable (as well as text surrounding variables) can only consist of alphanumerical characters and certain “special” characters, that being “$”, “@”, “#” and “-“.
Nor variables, nor the rest of the text could contain space characters (” “).

Now, the problem is that I’m trying to figure out a RegEx replacing one possible ending character (“.”), while not replacing the other possible ending character (“&”).
Which happanes to be quite an issue:

  • “&”+variable+”[^A-Za-z0-9#@$]” does what I want, except for it also replaces “&” – not acceptable.
  • “&”+variable+”(.)?\b” replaces dot, but only if followed by literal character – not if it’s followed by \&\@#\$\- and that could occur, so this doesn’t work either.
  • “&”+variable+”(.)?(?!A-Za-z0-9)” does exactly what i want as for the ending characters, except it doesn’t recognize true end of variable – this way, search-and-replace for “&DEN” also replaces that part in another variable, called “&DENV” – of which “&DEN” is a substring. This would create false/misleading results – totally unacceptable.

These were all the possibilities I could think of (and search of); is it possible to do the task I require with one RegEx at all? Under C#.NET RegEx parser?

Just to illustrate desired function:

string variable="DEN";
string replaceWith="28";
string replText;
string regex = "<desired regex>";
replText = Regex.Replace(replText, "&"+variable+regex, replaceWith);

replText="&DEN";

=> replaced => repltext==”28″

replText="&DENV"    

=> not replaced => repltext==”&DENV”

replText="&DEN&DEN"    

=> replaced => repltext==”2828″

replText="&DEN&DENV"    

=> replaced, not replaced => repltext==”28&DENV”

replText="&DEN.anything"

=> replaced and dot removed => repltext==”28anything”

replText="&DEN..anything"

=> replaced and first dot removed => repltext==”28.anything”

variable could also be like “#DE@N-$”.

  • 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-19T23:21:52+00:00Added an answer on May 19, 2026 at 11:21 pm

    The following works correctly on all of your examples. I assumed that a variable &FOO should only be replaced if it’s followed by ., &, or end-of-string $. If it’s followed by anything else, it’s not replaced.

    In order to match but not capture a terminating &, I used a lookahead assertion (?=&). Assertions force the string to match the regex, but they don’t consume any characters, so those characters aren’t replaced. Trailing . are still captured and replaced as part of the variable, however.

    Finally, a MatchEvaluator is specified to use the captured pattern to do a lookup in the replacements dictionary for the replacement value. If the pattern (variable name) is not found, the text is effectively untouched (the full original capture is returned).

    class Program
    {
        static string ReplaceVariables(Dictionary<string, string> replacements, string input)
        {
            return Regex.Replace(input, @"&([\w\d$@#-]+)(\.|(?=&)|$)", m =>
            {
                string replacement = null;
                return replacements.TryGetValue(m.Groups[1].Value, out replacement)
                     ? replacement
                     : m.Groups[0].Value;
            });
        }
    
        static void Main(string[] args)
        {
            string[] tests = new[]
            {
                "&DEN", "&DENV", "&DEN&DEN",
                "&DEN&DENV", "&DEN.anything",
                "&DEN..anything", "&DEN Foo",
                "&DEN&FOO&DEN"
            };
    
            var replace = new Dictionary<string, string>
            {
                { "DEN", "28" },
                { "FOO", "42" }
            };
    
            foreach (var test in tests)
            {
                Console.WriteLine("{0} -> {1}", test, ReplaceVariables(replace, test));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.