简体   繁体   English

如何使用正则表达式拆分字符串

[英]How do you use regex to split a string

how do i use regex to split a string such as Manga vol 1-515 into 2 different outputs i want Manga vol 1-5 as 1 output and 15 as another.我如何使用正则表达式将诸如Manga vol 1-515类的字符串拆分为 2 个不同的输出,我希望Manga vol 1-5作为 1 output 和15作为另一个。 i want to output them both into 2 separate text boxes the last 2 digits of the string will always be what i want to split.我想 output 将它们都放入 2 个单独的文本框中,字符串的最后 2 位数字将始终是我想要拆分的内容。 currently my solution is目前我的解决方案是

string pricepat = "[1,3,4][5,9,0]";
string Price = BtnGlobal.Text.ToString();
string priceMatches = Regex.Split(Price, pricepat);                
Pricetxt.Text = priceMatches.ToString();

For this case you can split it without Regex caption groups, and check that your string have at least two chars对于这种情况,您可以在没有正则表达式标题组的情况下拆分它,并检查您的字符串是否至少有两个字符

string input = "Manga vol 1-515";
string firstPart = input.Substring(0, input.Length - 2);
string lastTwo = input.Substring(input.Length - 2);

You can use Regex Match to get the last two characters您可以使用正则表达式匹配来获取最后两个字符

string input = "Manga vol 1-515";
var result = Regex.Match(input, @"(.{2})\s*$");

txtFirst.Text = input.Replace(result.ToString(), "");
txtSecond.Text = result.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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