I need to build a regex. the string i want to match always starts with \ then 4 or 5 numbers then another \
For example.
in first example i need "7778". In second example i need "7778". In third example i need "8278".
these 4 digit numbers is actually a port number, and its the only time on each line that this series of characters (eg, \7778\ ) would appear. sometimes the port number is 4 digits long, sometimes its 5.
I already know how to keep the string for later use using Regex.Match.Success, its just the actual regex pattern I am looking for here.
thanks
var match=Regex.Match(@"\1234\",@"\\(?<num>\d{4,5})\\");
if(match.Success)
{
var numString=match.Groups["num"].Value;
}
or (if you don't like using groups) you can use lookbehind and lookahead assertions to ensure your 4-5 digit match is surrounded by slashes:
var match=Regex.Match(@"\1234\",@"(?<=\\)\d{4,5}(?=\\)");
if(match.Success)
{
var numString=match.Value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With