本文整理汇总了C#中System.Text.RegularExpressions.Regex.Match方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.Match方法的具体用法?C# Regex.Match怎么用?C# Regex.Match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.Match方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test5
public void Test5()
{
var regex = new Regex(@"0*[1-9][0-9]*");
Assert.Equal("00077", regex.Match("00077").Value);
Assert.Equal("00077", regex.Match("00077bb").Value);
Assert.Equal("007", regex.Match("007").Value);
}
示例2: RegularTest
public void RegularTest()
{
Regex r = new Regex("^\\d+");
Console.WriteLine(r.Match("@").Success);
Console.WriteLine(r.Match("akb48").Success);
Console.WriteLine(r.Match("11akb48").Success);
}
示例3: GetScripts
/// <summary>
/// Gets all scripts that should be executed.
/// </summary>
/// <returns></returns>
public IEnumerable<SqlScript> GetScripts(Func<IDbConnection> connectionFactory)
{
var providerRegex = new Regex(@"_(?<Provider>.*?)\.");
return assembly
.GetManifestResourceNames()
.Where(filter)
.GroupBy(f=> providerRegex.Replace(f, "."))
.Select(g=>
{
if (g.Count() == 1)
return g.Single();
var matchingProvider = g.FirstOrDefault(
s =>
{
var @group = providerRegex.Match(s).Groups["Provider"];
return @group.Success &&
@group.Value == databaseProviderName;
});
return
matchingProvider ??
g.FirstOrDefault(s => !providerRegex.Match(s).Groups["Provider"].Success);
})
.OrderBy(x => x)
.Select(ReadResourceAsScript)
.ToList();
}
示例4: Parse
private ParamCollection Parse(ref string s, string pattern, bool remove, string tag, RegexOptions options)
{
ParamCollection cmd = new ParamCollection(Name);
Regex rex = new Regex(pattern, options);
Match m1 = rex.Match(s);
while (m1.Success)
{
string param = m1.Groups[PARAM].Value;
string arg = m1.Groups[ARGS].Value;
if (param != null)
{
param = param.TrimEnd(' ');
ArgCollection prm = cmd.Add(param, new ArgCollection(param, tag));
if (arg != null)
{
arg = arg.TrimEnd(' ');
if (!string.IsNullOrEmpty(arg))
{
prm.Add(arg);
}
}
}
if (remove)
{
s = s.Remove(m1.Index, m1.Length).Trim();
m1 = rex.Match(s);
}
else
{
m1 = rex.Match(s, m1.Index + m1.Length);
}
}
return cmd;
}
示例5: Preparse
public static string Preparse(string filename, string source)
{
Regex includeMatcher = new Regex("\\#include (.+)\n");
Match match = includeMatcher.Match(source);
while(match.Success)
{
string includeFile = Preparse(match.Groups[1].Value.Trim(), Media.ReadAllText(match.Groups[1].Value.Trim()));
source = source.Remove(match.Index, match.Length);
source = source.Insert(match.Index, includeFile + "\r\n");
match = includeMatcher.Match(source);
}
Regex includeOnceMatcher = new Regex("\\#include_once (.+)\n");
var included = new List<string>();
match = includeOnceMatcher.Match(source);
while(match.Success)
{
string file = match.Groups[1].Value.Trim();
if(included.Contains(file))
{
source = source.Remove(match.Index, match.Length);
}
else
{
included.Add(file);
string includeFile = Preparse(match.Groups[1].Value.Trim(), Media.ReadAllText(match.Groups[1].Value.Trim()));
source = source.Remove(match.Index, match.Length);
source = source.Insert(match.Index, includeFile + "\r\n");
match = includeMatcher.Match(source);
}
}
return PrependWithInfo(filename, source);
}
示例6: Main
static void Main(string[] args)
{
string s = "";
Regex r = new Regex(@"//");
bool loop = false;
while ((s = Console.ReadLine()) != null)
{
string temp = "";
if (r.IsMatch(s))
{
temp = s.Substring(r.Match(s).Index, s.Length - r.Match(s).Index);
Console.WriteLine(temp);
}
else if (s.Contains("*/"))
{
Console.WriteLine(s);
loop = false;
}
else if (loop)
{
Console.WriteLine(s);
}
else if (s.Contains("/*"))
{
Console.WriteLine(s);
loop = true;
}
}
Console.ReadLine();
}
示例7: ReferenceToColumnAndRow
/// <summary>
/// Converts references of form A1, B1, C3, DD99 etc to row and col
/// </summary>
/// <param name="reference"></param>
/// <returns>array of two elements 0 index is row num, 1 index is col. Note that the result is 1-based</returns>
public static int[] ReferenceToColumnAndRow(string reference)
{
//split the string into row and column parts
Regex matchLettersNumbers = new Regex("([a-zA-Z]*)([0-9]*)");
string column = matchLettersNumbers.Match(reference).Groups[1].Value.ToUpper();
string rowString = matchLettersNumbers.Match(reference).Groups[2].Value;
//.net 3.5 or 4.5 we could do this awesomeness
//return reference.Aggregate(0, (s,c)=>{s*26+c-'A'+1});
//but we are trying to retain 2.0 support so do it a longer way
//this is basically base 26 arithmetic
int columnValue = 0;
int pow = 1;
//reverse through the string
for (int i = column.Length - 1; i >= 0; i--)
{
int pos = column[i] - 'A' + 1;
columnValue += pow * pos;
pow *= 26;
}
return new int[2] { int.Parse(rowString), columnValue };
}
示例8: coder_DoWork
private static void coder_DoWork(object sender, DoWorkEventArgs e)
{
var i = 0;
switch (Processor.Lang)
{
case Languages.Ru:
foreach (var c in Processor.Text.Where(char.IsLetter))
{
var reg = new Regex(Processor.Key[i].ToString());
var reg1 = new Regex(c.ToString());
Processor.Cipertext += char.IsLower(c)
? Processor.SmlRus[((reg1.Match(Processor.SmlRus).Index) + reg.Match(Processor.SmlRus).Index) % 33]
: Processor.BigRus[((reg1.Match(Processor.BigRus).Index) + reg.Match(Processor.SmlRus).Index) % 33];
i++;
(sender as BackgroundWorker).ReportProgress(i);
}
break;
case Languages.Eng:
foreach (var c in Processor.Text.Where(char.IsLetter))
{
Processor.Cipertext += char.IsLower(c)
? Processor.SmlEng[((c - 'a') + (Processor.Key[i] - 'a')) % 26]
: Processor.BigEng[((c - 'A') + (Processor.Key[i] - 'a')) % 26];
i++;
(sender as BackgroundWorker).ReportProgress(i);
}
break;
}
}
示例9: SFButtonClick
//When 'Go' button is clicked / main function
protected void SFButtonClick(object sender, EventArgs e)
{
//Regext check input
Regex numeric09 = new Regex(@"^[0-9]+$");
Regex alphaAZ = new Regex(@"^[A-Za-z]+$");
Match matchNumberic09n = numeric09.Match(numeratorInputTxtBox.Text.ToString().Trim());
Match matchalphaAZn = alphaAZ.Match(numeratorInputTxtBox.Text.ToString().Trim());
Match matchNumberic09d = numeric09.Match(denominatorInputTxtBox.Text.ToString().Trim());
Match matchalphaAZd = alphaAZ.Match(denominatorInputTxtBox.Text.ToString().Trim());
//When regex matches
if (matchNumberic09n.Success && matchNumberic09d.Success)
{
int numerator;
Int32.TryParse(numeratorInputTxtBox.Text.ToString().Trim(), out numerator);
int denominator;
Int32.TryParse(denominatorInputTxtBox.Text.ToString().Trim(), out denominator);
int GCD = numGCD(numerator, denominator);
outputLabel.Text = (numerator / GCD).ToString() + " / " + (denominator / GCD).ToString();
}
else if (matchalphaAZn.Success && matchalphaAZd.Success)
outputLabel.Text = lettersFraction(numeratorInputTxtBox.Text.ToString().Trim(), denominatorInputTxtBox.Text.ToString().Trim());
else
outputLabel.Text = "Incorrect Input! Please try again!";
}
示例10: Index
public ActionResult Index(PatBasicInfor pat)
{
PatOperation pto = new PatOperation();
string PID = Request.Form["门诊号"];
if (string.IsNullOrEmpty(pat.Name) || string.IsNullOrEmpty(pat.Sex) || string.IsNullOrEmpty(pat.Age) || string.IsNullOrEmpty(pat.Phone))
{
ModelState.AddModelError("", "带*的输入项不能为空");
}
else
{
Regex reg = new Regex("^[0-9]+$");
Match ma1 = reg.Match(pat.Age);
Match ma2 = reg.Match(pat.Phone);
if (ma1.Success && ma2.Success)
{
}
else
{
ModelState.AddModelError("", "手机和年龄必须为数字");
}
}
if (ModelState.IsValid)
{
string user;
user = HttpContext.Request.Cookies["username"].Value.ToString();
pto.InsertPat(pat, PID, user);
return RedirectToAction("Index", "Diagnosis", new { ID = pat.Id });
}
else
{
return View();
}
}
示例11: ProcessContent
public string ProcessContent(string text, string flavor)
{
if (string.IsNullOrEmpty(text))
return string.Empty;
text = Regex.Replace(text.Replace(FAQGroupStart, FAQGroupStartReplaced), FAQGroupEndRegex, "[/faq_group]", RegexOptions.Multiline);
text = Regex.Replace(text.Replace(FAQStart, FAQStartReplaced), FAQEndRegex, "[/faq]", RegexOptions.Multiline);
var regex = new Regex(GetShortcodeRegex("faq_group"), RegexOptions.None);
var sb = new StringBuilder(text);
var index = 0;
var match = regex.Match(text);
while (match.Success)
{
var value = match.Value;
// Do FAQ Group
var sbToManipulate = FormatFAQGroup(match, index);
FormatFAQ(index, sbToManipulate);
sb = sb.Replace(
sb.ToString().Substring(match.Index, match.Length),
sbToManipulate.ToString(),
match.Index,
match.Length);
match = regex.Match(sb.ToString());
index++;
}
return sb.ToString();
}
示例12: Build
public string Build(object parameter)
{
var data = parameter.Convert();
foreach (string variable in Options.VariableCollection)
{
if (string.IsNullOrEmpty(data[variable]))
{
data.Add(variable, Options.VariableCollection[variable]);
}
}
Match[] results = Parse();
string temp = TemplateContent;
foreach (string item in data.Keys)
{
var regx = new Regex(string.Format(ItemRegxFormat, item), RegxOptions);
List<Match> collection = results.Where(t => t.Success && regx.Match(t.Value).Success).ToList();
string value = data[item];
foreach (Match match in collection)
{
Match subMatch = regx.Match(match.Groups[1].Value);
var words = new Regex(@"\w", RegxOptions);
string g1 = subMatch.Groups[1].Value;
string g2 = subMatch.Groups[2].Value;
string g3 = subMatch.Groups[3].Value;
if (words.Match(g1).Success || words.Match(g3).Success || string.IsNullOrEmpty(g2))
{
continue;
}
temp = temp.Replace(match.Value, value);
}
}
return temp;
}
示例13: button1_Click
private void button1_Click(object sender, RoutedEventArgs e)
{
string strPattern = @"CM-\w+";
string strToTest = "CM-2345 implemented";
string strToTest2 = "Changes in CM-2395";
string strToTest3 = "Changes for CM-2365 complete and need to change CM-1258 ";
var regEx = new Regex(strPattern);
var match1 = regEx.Match(strToTest);
MessageBox.Show(match1.Success ? match1.Groups[0].Value : "not match " + strToTest);
var match2 = regEx.Match(strToTest2);
MessageBox.Show(match2.Success ? match2.Groups[0].Value : "not match " + strToTest2);
var matches3 = regEx.Matches(strToTest3);
foreach (Match match3 in matches3)
{
foreach (Capture capt in match3.Captures )
{
MessageBox.Show(string.Format("Index={0}, Value={1}", capt.Index, capt.Value));
}
}
//MessageBox.Show(match3.Success ? match3.Groups[0].Value : "not match " + strToTest3);
//MessageBox.Show(match3.Success ? match3.Groups[1].Value : "not match " + strToTest3);
}
示例14: Test7
public void Test7()
{
var regex = new Regex(@"^\//(\[{1}[^0-9]+\]{1})+[\n]+");
Assert.Equal("//[***]\n", regex.Match("//[***]\n3***4***5***").Value);
Assert.Equal("//[***][--]\n", regex.Match("//[***][--]\n3***4***5***").Value);
Assert.False(regex.IsMatch("22//[***]"));
}
示例15: DetectCharset
/// <summary>
/// 自动检测内容的charset,通过字符集和html中指定的contentType来获取
/// 如果无法获得字符集,则返回null
/// </summary>
/// <param name="stringcontent"></param>
/// <returns></returns>
public static Encoding DetectCharset(QuickWebResponse response, byte[] bytes)
{
string regexstr = "(text/html|text/xml).*charset=(?<charset>\\w+\\-*\\d*)";
Regex regex = new Regex(regexstr, RegexOptions.IgnoreCase);
string contentType = response.ContentTypeStr;
if (contentType != null)
{
Match match1 = regex.Match(contentType);
if (match1.Success)
{
string charset = match1.Groups["charset"].Value.ToUpper();
Encoding encoder = System.Text.Encoding.GetEncoding(charset);
if (encoder != null)
{
return encoder;
}
}
}
string ascii = System.Text.Encoding.ASCII.GetString(bytes);
//<META http-equiv="Content-Type" content="text/html; charset=GB2312">
//Content-Type=text/html;
Match match = regex.Match(ascii);
if ( match.Success )
{
string charset = match.Groups["charset"].Value.ToUpper();
return System.Text.Encoding.GetEncoding(charset);
}
else {
return null;
}
}