日期:2014-05-18  浏览次数:21041 次

请问,这个正则表达式怎么写?
string s = "百位邻期方案23";

要求:
如果字符串s中具有“方案”二字,就取“方案”以前的字符串。
如果没有“方案”二字,就全取字符串s。


正则表达式该怎么写?

------解决方案--------------------
if(s.indexOf("方案")!=-1)
------解决方案--------------------
C# code

Regex reg = new Regex(@"(?is)(?(.*方案).*?(?=方案)|.*)");


            string source = "百位邻期方案23";           
            string replacestr = @"";
            Regex reg = new Regex(@"(?is)(?(.*方案).*?(?=方案)|.*)");
            Match mm = reg.Match(source);
            MessageBox.Show(mm.Value);

------解决方案--------------------
C# code

new Regex("(?is)((.*方案)|(.*))")

------解决方案--------------------
看错了,如果是提取方案以前字符串的话,二楼正确的
------解决方案--------------------
这个也不一定非得正则啊,,字符串截取也可以的
------解决方案--------------------
string s = "百位邻期方案23";
string regx = "(.*)方案|(.*)";
Match one = Regex.Match(s, regx);
Console.WriteLine(one.Groups[1].Value == "" ? one.Groups[2].Value : one.Groups[1].Value);
------解决方案--------------------
C# code
^(?:(?!方案).)+