I have a string which I want to examine and search for a substring within it. If the substring is found, I want to do something on the original string.
The string looks like this:
"\r\radmin@Modem -- *<456> \radmin@Modem -- *<456> "
Goal: Search the substring pattern " -- *<456> " if it exists in the string, and return success or fail (the digits number is between 1 to infinite: 1, 5, 36, 76, 478, 975 etc.).
What is the regular expression pattern which I need?
Use this:
var myRegex = new Regex("(?<=<)[0-9]+(?=>)");
string resultString = myRegex.Match(yourString).Value;
Console.WriteLine(resultString);
// matches 456
See the match in the Regex Demo.
Explanation
(?<=<) asserts that what precedes is <[0-9]+ matches one or more digits(?=>) asserts that what follows is >You can use this following piece of code to check if your pattern exist :
string yourInput = "\r\radmin@Modem -- *<456> \radmin@Modem -- *<456> " ;
string pattern = @"<(\d+)>";
boolean success = Regex.Match(yourInput , pattern, RegexOptions.IgnoreCase).Success ;
success will be true if a number is found.
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