简体   繁体   中英

Using regex to split pattern

I am new to using pattern in regular expression. I have read a couple of links from Microsoft site, thought I understood but I came across this scenario and do not know why it is not providing the results I expected.

I would like to split MyCmd into a list of strings: print,a,+,b,; Normal split will not keep the delimiters as far as I understand. So, I think I have tried using regex with the pattern defined below: (basically I want to split the string into a list or queue and keep the delimiters ;,+-*/{}[]).

 string MyCmd = "print a+b;";
 private string MyDelim = @"\b[\s;,\+\-\*\/%=\<\>\(\)\{\}\[\]]\w+";
 myStuff = new Queue<string>(Regex.Split(MyCmd,MyDelim));

But so far, my code above is not yielding the expected results.

What is not correct in my pattern?

I believe you can use

var MyCmd = "print a+b;";
var MyDelim = @"([][\s;,+*/%=<>(){}-])";
var myStuff = Regex.Split(MyCmd,MyDelim).Where(p=> !string.IsNullOrWhiteSpace(p)).ToList();

Output: print , a , + , b , ;

在此处输入图片说明

Note that the ([][\\s;,+*/%=<>(){}-]) regex is enclosed with (...) and that capturing group makes sure the captured values also get added to the resulting array.

You need the .Where(p=> !string.IsNullOrWhiteSpace(p)) to get rid of empty values that you will get with Regex.Split .

I removed excessive escaping in your regex so that it looks "lean and mean".

The reason your regex does not work is that @"\\b[\\s;,\\+\\-\\*\\/%=\\<\\>\\(\\)\\{\\}\\[\\]]\\w+" matches the spaces and symbols in the character class after a \\b word boundary (requiring a word character to appear before them) and then it matched one or more word characters. Since there is no capturing group, all the matches disappeared from the resulting array.

Regex.Split is the way to go. If using Regex.Matches , you can do like this:

string MyCmd = @"print  a+b;";
string MyDelim = @"([^;,+\-*/{}\[\]\)\(\s]+|[;,+\-*/{}\[\]\)\(])";
var myStuff = Regex.Matches(MyCmd, MyDelim).Cast<Match>().ToList().ConvertAll(m => m.Groups[1].Value.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM