I've searched around a bit, but it's hard to tell if this exact question has been answered before. I know you'll let me know if this is a duplicate.
I have a regular expression that matches a series of one or more positive integers preceded by a backslash. For example: \12345 would match, but \1234f or 12345 would not match.
The regex I'm using is ^\\(\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 fail to get a match.
The 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;
}
The failing 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);
}
The test's 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 it is passing tests now.
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;
}