Regular expression working in regex test but not in C #

I've searched a bit, but it's hard to tell if this exact question was there before. I know you will let me know if this is a duplicate.

I have a regex that matches a series of one or more positive integers, preceded by a backslash. For example: \ 12345 will match, but \ 1234f or 12345 will not match.

I am using regex ^ \\ (\ d +) $

When I test the expression using various testers, it works. For example see http://regex101.com/r/cY2bI1/1

However, when I implement it in the following C # code, I cannot get a match.

Implementation:

public string ParseRawUrlAsAssetNumber(string rawUrl) {
    var result = string.Empty;
    const string expression = @"^\\([0-9]+)$";
    var rx = new Regex(expression);
    var matches = rx.Matches(rawUrl);
    if (matches.Count > 0)
    {
        result = matches[0].Value;
    }
    return result;
}

      

Failed test (NUnit):

[Test]
public void ParseRawUrlAsAssetNumber_Normally_ParsesTheUrl() {
    var f = new Forwarder();
    var validRawUrl = @"\12345";
    var actualResult = f.ParseRawUrlAsAssetNumber(validRawUrl);
    var expectedResult = "12345";
    Assert.AreEqual(expectedResult, actualResult);
}

      

Test output:

Expected string length 5 but was 6. Strings differ at index 0.
Expected: "12345"
But was:  "\\12345"
-----------^

      

Any ideas?

Resolution:

Thanks everyone for the input. In the end I took the following route based on your recommendations and is currently undergoing tests.

public string ParseRawUrlAsAssetNumber(string rawUrl)
{
    var result = string.Empty;
    const string expression = @"^\\([0-9]+)$";
    var rx = new Regex(expression);
    var matches = rx.Matches(rawUrl);
    if (matches.Count > 0)
    {
        result = matches[0].Groups[1].Value;
    }
    return result;
}

      

+1


source to share


3 answers


The problem is this line:

var rx = new Regex(Regex.Escape(expression));

      

By exiting the expression, you convert all of your regex special characters to literals. The call Regex.Escape(@"^\\(\d+)$")

will return "\^\\\\\(\\d\+\)\$"

which will only match the literal string"^\\(\d+)$"

Try the following:

var rx = new Regex(expression);

      

See MSDN: Regex.Escape

for a full explanation and examples of how this method is intended to be used.




Given your updated question, it looks like you also have a problem:

result = matches[0].Value;

      

This will return the entire substring, not just the first capture group. To do this, you will need to use:

result = matches[0].Groups[1].Value;

      

+7


source


Don't leave a template. Also just use Regex.Match

this way you will have one match here. Use Match.Success

to check if the input matches your pattern. And return the group value - the numbers are in the group of your matched expression:



public string ParseRawUrlAsAssetNumber(string rawUrl)
{            
    const string pattern = @"^\\(\d+)$";

    var match = Regex.Match(rawUrl, pattern);
    if (!match.Success)
        return String.Empty;

    return match.Groups[1].Value;
}

      

+3


source


What if you try to get a group result?

match.Groups[1].Value

When I get to a real computer I will be testing but it looks like it should work

+1


source







All Articles