111,129
社区成员
发帖
与我相关
我的任务
分享
string myString = "xxxxSHENBAOyyy";
//正声明(?=yyy) 负声明(?!xxxx)声明本身不作为匹配结果的一部分
Regex myRegex = new Regex(@"SHENBAO(?=yyy)(?!xxxx)");
Console.WriteLine(myRegex.Match(myString).Value);
//输出结果:SHENBAO
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "asdfxxx32\r\n1y\r\ny31yyyy";
Regex r = new Regex(@"xxx([\w\W]*?)yyy");
Match m = r.Match(str);
Console.Write(m.Groups[1].ToString());
Console.ReadLine();
}
}
}
string pattern = "(?is)(?<=" + Regex.Escape("前面")+").+?(?="+Regex.Escape("后面")+")";
Regex reg = new Regex(pattern);
reg.Match(yourStr).Value;//就是你要的 public static List<string> GetStringsBetween(string content, string beginString, string endString, bool ignoreCase)
{
List<string> list = new List<string>();
string pattern = "(?<=" + beginString + ").*(?=" + endString + ")";
RegexOptions ro = RegexOptions.Singleline;
if (ignoreCase)
ro = RegexOptions.IgnoreCase | RegexOptions.Singleline;
MatchCollection mats = Regex.Matches(content, pattern, ro);
foreach (Match mat in mats)
{
list.Add(mat.Value);
}
return list;
} public static List<string> GetStringsBetween(string content, string beginString, string endString, bool ignoreCase)
{
List<string> list = new List<string>();
string pattern = "(?<=" + beginString + ").*(?=" + endString + ")";
RegexOptions ro = RegexOptions.Singleline;
if (ignoreCase)
ro = RegexOptions.IgnoreCase | RegexOptions.Singleline;
MatchCollection mats = Regex.Matches(content, pattern, ro);
foreach (Match mat in mats)
{
list.Add(mat.Value);
}
return list;
}
static void Main()
{
string str = @"123hjkl456
qwer
789 tyui
&jsad#op
zxc@v
mnb v567cxz";
string str1 = @"123hjkl456
qwer
789 tyui
&";
string str2 = @"@v
mnb v567cxz";
List<string> list = GetStringsBetween(str, str1, str2, true);
}(?<=xxx).*?(?=yyy)