Search code examples
c#regexvisual-studio-2012infopath

Regular Expression not Working


I am receiving some strings in this format:

"tel:+441234567890;ext=7890"

To extract the different numbers, I usually use these regex patterns, which return the shown output:

ParseTelephoneNumber

public static string ParseTelephoneNumber(string rawInput)
{
    var temp = Regex.Replace(rawInput, ";.*", "");
    return Regex.Replace(temp, ".*:", "");
}

+441234567890

ParseExtension

public static string ParseExtension(string rawInput)
{
    return Regex.Replace(rawInput, ".*=", "");
}

7890


This seems to work fine in both Visual Studio 2012 and Visual Studio 2015.

However, if this code runs in VS 2012 as part of the code for an InfoPath 2013 form, I see these results:

ParseExtension

tel:+441234567890

ParseTelephoneNumber

+441234567890

What is the reason for this? Does InfoPath use a different engine for calculating regular expressions?


Solution

  • Is your original string immutable in InfoPath I assume not, that is why you are seeing this behavior,
    If not are you sure InfoPath is passing the string in the same format to your C# code Else can you try something like below ?

    public static Tuple<string, string> ParseTelephoneAndExtension(string rawInput)
    {
        var match = Regex.Match(rawInput, @"tel:(\+\d+);ext=(\d+)");
        if (match.Success)
        {
            return new Tuple<string, string>(match.Groups[1].Value, match.Groups[2].Value);
        }
        return new Tuple<string, string>(null, null);
    }
    

    Also regex is not VS dependent, it is more of a .NET engine. As long as you send the string in the same format, you should get the same output.