user3657339
user3657339

Reputation: 627

Regex Match expression include end word

String = "shall not exceed 10 per cent. per annum/that applicable "

Regex I'm trying
(?<=shall not exceed)(.*)(?=per annum)

Output Required = 10 per cent. per annum
Output coming = 10 per cent.

But with above regex it doesnt include "per annum", how to include "per annum". We are doing this in asp.net

Code in Asp.net we use...

            string regularExpressionPattern = regExPattern.ToString();
            string inputText = inputString
            Regex re = new Regex(regularExpressionPattern);
            foreach (Match m in re.Matches(inputText))
            {
                Response.Write(m.Groups[1].Value.ToString());
            }

Upvotes: 1

Views: 50

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Make the per annum a part of the consuming pattern since when it is inside a positive lookahead the text matched is not added to the whole match value.

You may use

string inputText = "shall not exceed 10 per cent. per annum/that applicable";
Regex re = new Regex("shall not exceed(.*?per annum)");
foreach (Match m in re.Matches(inputText))
{
    Console.WriteLine(m.Groups[1].Value);
}

See C# demo

Details

  • shall not exceed - a literal string
  • (.*?per annum) - Capturing group 1:
    • .*? - 0+ chars other than newline, as few as possible
    • per annum - a literal string.

The Group 1 value can be accessed via m.Groups[1].Value.

Upvotes: 1

Related Questions