c# - Match Multiple IP Adresses per line -


i try match string regex having rule start 10.20

string str = @"10.20.30.1\r\n10.20.40.2\r\n10.20.50.3"; string pattern = @"(10\.20.+(\r\n)*)+";  var m = system.text.regularexpressions.regex.match(str, pattern); 

however catches first line, i.e.:

console.write(m.tostring()); // prints 10.20.30.1, 

edit: try differentiate case there single or multiple lines. i.e. in above example of str, if user gives

string pattern = @"(10\.20.+)+"; 

it matches first line, expect. missing?

have tried using matches instead of match. you're looking multiple matches:

var matches = system.text.regularexpressions.regex.matches(     "10.20.30.1\r\n10.20.40.2\r\n10.20.50.3",     @"(10\.20.+(\r\n)*)+",     regexoptions.compiled | regexoptions.cultureinvariant);  assert.areequal(3, matches.count); 

in reply comment below, matches string:

var match = system.text.regularexpressions.regex.match(         "foo 10.20.30.1\r\n10.20.40.2\r\n10.20.50.3 bar",         @"(10\.20\.\d{1,3}\.\d{1,3}(\r\n)*)+",         regexoptions.compiled | regexoptions.cultureinvariant)     .tostring(); 

it's best use regex specific possible, still matches criteria. since . matches except \n, there's still solution come @"(10\.20\.([^\r])+(\r\n)*)+". though match 10.20.30.1\r\n10.20.40.2\r\n10.20.50.3 bar if still have other information around it.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -