With these data examples:
/test -test/test/2016/April
/test -test/test/2016
How does one pattern match so that it can determine whether or not the number 2016 is located in this exact position?
With these data examples:
/test -test/test/2016/April
/test -test/test/2016
How does one pattern match so that it can determine whether or not the number 2016 is located in this exact position?
Assuming, that "exact position" means "third position", the following regex would work:
/(?:[^/]*/){2}(\d{4}).*
In C#, this can be used with the Regex Constructor and the @"" String Syntax, which makes escaping characters obsolete:
var rx = new Regex(@"/(?:[^/]*/){2}(\d{4}).*");
If this regex matches a string, the four digits of the year are captured as a result.
Explanation
/
captures the leading slash character.
[^/]*
captures any sequence of characters unequal to a slash.
/
captures a slash character
the preceeding two code parts are now wrapped inside non-capturing brackets, which are specified with ?:
as the first two characters inside them.
Having (?:[^/]*/)
now matching a "path segment" like "test/", the pattern must be matched exactly two times in a row. that's why the brackets are followed by the quantifier {2}
Then the actual number must be matched: It consists of four digits in a row. This is represented as followed: (\d{4})
where \d means "any number" and - once again - the quantifier defines that there should be 4 in a row.
Finally, there can be aribtrary characters behind the number, ("tha path can continue"): This is specified by the .
("match any character") and the quantifier *
, which means "any number of occurences".
Note: There are many dialects of Regular Expressions. This on works for the C# regex implemantation, however it should work for many others as well.
A regex pattern can do validation or as you infer location positioning validation. The key is to setup pattern anchors based on the strings encountered before one gets to just the numeric.
For your case you have literal /
s then text then a literal -
then literal /
s then text....etc. By following those patterns of the literal anchors with generic text, you can require a specific position.
But other numbers could spoof other patterns (noise per se), so you appear to be getting a date. The following will make sure that /{date of 19XX or 20XX}/
is the only valid item for that position.
string pattern = @"
^ # Beginning of line (anchor)
/ # / anchor
[^-]+ # Anything not a dash.
- # Anchor dash
[^/]+ # Anything not a /
/ # / anchor
[^/]+ # Anything not a /
/ # / anchor
[12][90]\d\d # Allow only a `date` field of 19XX or 20XX.
";
// IgnorePatternWhitespace *only* allows us to comment the pattern
// and place it on multiple lines (space ignored)
// it does not affect processing of the data.
// Compiled tells the parser to hold the pattern compilation
// in memory for future processing.
var validator = new Regex(pattern, RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled);
validator.IsMatch("/ -test/test/2016/April"); // True
validator.IsMatch("/ -test/test/2016"); // True
validator.IsMatch("/ -test/test/1985/April"); // True
validator.IsMatch("/ -2017/test/1985/April"); // True
// Negative Tests
validator.IsMatch("/ -2017/test/WTF/April"); // False
validator.IsMatch("/jabberwocky/test/1985/April"); // False, no dash!
validator.IsMatch("////April"); // false
validator.IsMatch("///2016/April"); // False because no text between `/`
validator.IsMatch("/ -test/test/ 2016/April"); // False because pattern
// does not allow a space
Pattern Notes
\d\d\d\d
, I am giving the regex parser a specific anchor type hint that this is either going to be a date in that resides in the twentieth century, 19XX, or the twenty first century, 20XX. So I spell out the first two places of the \d\d\d\d
pattern to be a set where either 1 or 2 is the first \d
as [12]
(1 for a 19xx pattern or 2 for a 20xx pattern) followed by the second place number to be either a nine or a zero[90]
. In a modern computer system most dates will be within these two centuries; so why not craft the regex as such.Your regex will be:
\-(?:[^\/]+\/){2}(\d+)
It will capture number appearing after xx/xx/
pattern where xx/
is adjustable.
Example:
var s1 = "/test -test/test/2016/April";
var s2 = "/test -test/test/2016";
var rx = new Regex ("\\-(?:[^\\/]+\\/){2}(\\d+)");
var m1 = rx.Match(s1);
var m2 = rx.Match(s2);
if (m1.Success && m2.Success) {
if (m1.Groups[1].Value == m2.Groups[1].Value) {
Console.WriteLine ("s1 == s2");
}
}
Based on provided input string s1
and s2
, it will print:
s1 == s2