Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Possible to Perform Addition in a Regex?

Tags:

regex

asp.net

Assuming the placeholder $2 is populated with an integer, is it possible to increment it by 1?:

var strReplace = @"$2";
Regex.Replace(strInput, @"((.)*?)", strReplace);
like image 385
StronglyTyped Avatar asked Mar 08 '12 13:03

StronglyTyped


People also ask

What is the difference between or and regex?

Regex (Regular Expression) OR Logic Alternation Tutorial with Examples. A regular expression is a formation in order to match different text or words or numbers according to the given regex pattern. OR is a logic term used to provide selection choice from multiple choices.

How do you use a regex in a form?

A regex usually comes within this form / abc /, where the search pattern is delimited by two slash characters /. At the end we can specify a flag with these values (we can also combine them each other): g (global) does not return after the first match, restarting the subsequent searches from the end of the previous match.

How to limit the end of the regex to the line?

Regular expression targets only provided regular expression and do not check previous or after characters. If we want to limit the end of the regex and be sure that the line ends we can add $ at the end of the regular expression. We will use the following regular expression. Regex OR is very useful in order to match multiple IP addresses.

How do you build a logical “AND” operation using regular expressions?

When attempting to build a logical “and” operation using regular expressions, we have a few approaches to follow. The first approach may seem obvious, but if you think about it regular expressions are logical “and” by default. Every sequential character in a regular expression is “and’ed” together.


2 Answers

You can use a callback version of Regex.Replace with a MatchEvaluator, see examples at:

  • http://msdn.microsoft.com/en-us/library/cft8645c.aspx
  • http://www.dotnetperls.com/regex-replace

Here's an example (ideone):

using System;
using System.Text.RegularExpressions;

class Program
{
    static string AddOne(string s)
    {
        return Regex.Replace(s, @"\d+", (match) =>
        {
            long num = 0;
            long.TryParse(match.ToString(), out num);
            return (num + 1).ToString();
        });
    }

    static void Main()
    {
        Console.WriteLine(AddOne("hello 123!"));
        Console.WriteLine(AddOne("bai bai 11"));
    }
}

Output:

hello 124!
bai bai 12
like image 73
Qtax Avatar answered Sep 27 '22 19:09

Qtax


In standard (CS theoretic) regular expressions, it is impossible with a regular expression.

However, Perl and such have extensions to Regular Expressions, which has implications for their behaviour, and I am not familiar enough with them to definitely say that the extended regexes will not do it, but I'm fairly sure that this behaviour is not possible with a regex.

like image 33
Tinned_Tuna Avatar answered Sep 27 '22 18:09

Tinned_Tuna