Combine a Regular Expression pattern
following sample code check string 2 regular expression patterns.
using system.collections.generic; using system.io; using system.text; using system.text.regularexpressions; namespace consoleapp { internal class program { private static void main() { string[] contents = file.readalllines("book.txt",encoding.default); var = new list<string>(); var rx = new regex("^\\d{1,2}\\.([a-za-z0-9]{1,}|\\d{1,2}|[ ]{1,})|^[a|b]{1}\\.(\\d{1,2})"); var rx1 = new regex("\\d$"); foreach (string content in contents) if (rx.ismatch(content) && rx1.ismatch(content)) a.add(string.format("[*{0}*]", content)); else a.add(content); file.writealllines("newbook.txt", a.toarray(), encoding.default); } } }
from above combine simple expression
var rx = new regex("^\\d{1,2}\\.([a-za-z0-9]{1,}|\\d{1,2}|[ ]{1,})|^[a|b]{1}\\.(\\d{1,2})"); var rx1 = new regex("\\d$");
into single pattern.
that combining regular expression pattern rx , rx1 single regular expression pattern.
arun kumar non ascii
hi murray,
var rx = new regex("^\\d{1,2}\\.([a-za-z0-9]{1,}|\\d{1,2}|[ ]{1,})|^[a|b]{1}\\.(\\d{1,2})");
where pattern rx checks string start number a|b.
in condition
var teststrings = new string[] { "12.thisisatest$", "12.thisisatest9", "12.1234 4", "12.1234 $", "12.c.12abcdef", "12.a.12abcdef", "12.a.12abcdef9","1. introduction","1.1 hello world","1.2 program structure","a.1 introduction","a.2 recommended tags","a.2.1 <c>","a.2.8 <param>" };
its print on console "bad"...
but you're mere solution regular expression shuould be
var rx = new regex("(^\\d{1,2}\\.([a-za-z0-9]{1,}|\\d{1,2}|[ ]{1,})|^[a|b]{1}\\.(\\d{1,2}))(.*?)(\\d$)");
arun kumar non ascii
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment