[英]Regex cut the words out of the sentence C#
I have some sentences that I would like to extract some words by Regex in C# 我有一些句子,我想在C#中提取Regex的一些单词
Example like: 像这样的例子:
// Standard room 1 persons from 18/03/2014 to 19/03/2014 // 标准房1人,从18/03/2014至19/03/2014
Get the "Standard room 1 persons" out of the sentence, remove from xxxx to xxxx 从句子中取出“ Standard room 1 person”,从xxxx删除到xxxx
// Family night room 3/4 from 18/03/2014 to 19/03/2014 //从2014年3月18日至2014年3月19日的家庭夜间3/4
Get the "Family night room" and 3/4 is the number of persons in the room and I would like to take the 4 out to be a maximum number of person in the room. 取得“家庭房”,房间的人数是3/4,我想把4取为房间的最大人数。
For both cases, the from and to are ignored. 对于这两种情况,from和to都将被忽略。
Could you please suggest me the reg ex pattern to do those things ( 2 cases )? 您能否建议我使用reg ex模式来做那些事情(2例)?
Thank you very much and have a nice day! 非常感谢,祝您有美好的一天!
If you don't insist on Regex: 如果您不坚持使用正则表达式:
string[] array1 = "Standard room 1 persons from 18/03/2014 to 19/03/2014".Split();
string[] array2 = "Family night room 3/4 from 18/03/2014 to 19/03/2014".Split();
string n = String.Join(" ", array1.TakeWhile(s => s != "from").ToArray());
string n2 = String.Join(" ", array2.TakeWhile(s => s != "from").ToArray());
Console.WriteLine(n); // Standard room 1 persons
Console.WriteLine(n2); // Family night room 3/4
Simply use this: 只需使用以下命令:
string s = "//Standard room 1 and 1/4 persons from 18/03/2014 to 19/03/2014";
string matchedStr = Regex.Match(s, ".*(?=from)(?=.*to)").Value;
Console.WriteLine(matchedStr);
string totalStr = Regex.Match(s, ".*(?=from)(?=.*to)").Value;
int total=Convert.ToInt32(Regex.Match(totalStr,@"(?<=\d/)\d").Value);
Console.WriteLine(total);
Sample code: 样例代码:
string input = "Standard room 1 persons from 18/03/2014 to 19/03/2014"
string s = Regex.Match(input, "(.*)(?=from)").Value;
s = s.Trim();
// Get your maximum number
MatchCollection numbers = Regex.Matches(s, "[\d]");
int max = 0;
foreach (Match number in numbers)
{
int temp = int.Parse(number.Value);
if (max > temp)
max = temp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.