本文整理汇总了C#中Regex.Match方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.Match方法的具体用法?C# Regex.Match怎么用?C# Regex.Match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Regex
的用法示例。
在下文中一共展示了Regex.Match方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
var patern =
@"\s*<\s*([a-z]+)\s+(?:value\s*=\s*""\s*(\d+)\s*""\s*)?(?:[a-z]+\s*=\s*""([^""]*)"")?\s*\/>\s*";
Regex rgx = new Regex(patern);
var input = Console.ReadLine();
var match = rgx.Match(input);
int lineNumber = 1;
while (input != "<stop/>")
{
var tag = match.Groups[1].Value;
switch (tag)
{
case "inverse":
Inverse(ref lineNumber, match.Groups[3].Value);
break;
case "reverse":
Reverse(ref lineNumber, match.Groups[3].Value);
break;
case "repeat":
Repeat(ref lineNumber, int.Parse(match.Groups[2].Value), match.Groups[3].Value);
break;
}
input = Console.ReadLine();
match = rgx.Match(input);
}
}
示例2: AtomicGroup
public void AtomicGroup()
{
Regex re = new Regex("(?>.*/)foo");
Assert.That(re.Match("/this/is/a/very/long/line/in/deed/with/very/many/slashes/in/and/foo").ToString(),
Is.EqualTo("/this/is/a/very/long/line/in/deed/with/very/many/slashes/in/and/foo"));
Assert.False(re.Match("/this/is/a/very/long/line/in/deed/with/very/many/slashes/in/it/you/see/").IsSuccess);
}
示例3: BasicComplement
public void BasicComplement()
{
Regex re = new Regex("^(~.*ab.*)$", RegexOptions.DotAll);
Assert.That(re.Match("bbaaa").ToString(), Is.EqualTo("bbaaa"));
Assert.That(re.Match("aaa").ToString(), Is.EqualTo("aaa"));
Assert.False(re.Match("aaabb").IsSuccess);
}
示例4: DotAllModeOff
public void DotAllModeOff()
{
Regex re = new Regex("^12(?-s).34", RegexOptions.DotAll);
Assert.That(re.Match("12.34").ToString(), Is.EqualTo("12.34"));
Assert.False(re.Match("12\n34").IsSuccess);
Assert.False(re.Match("12\r34").IsSuccess);
}
示例5: Main
static void Main()
{
StreamReader reader = new StreamReader(@"..\..\Files\input.txt");
StreamWriter writer = new StreamWriter(@"..\..\Files\output.txt");
Regex testWord = new Regex(@"test\w+");
string line;
using (reader)
{
using (writer)
{
while ((line = reader.ReadLine()) != null)
{
for (int i = 0; i < line.Length - 1; i++)
{
int numberOfCharsToRead = line.IndexOf(' ', i + 1) - i;
if(numberOfCharsToRead < 0 && testWord.Match(line.Substring(i)).Success)
{
line = line.Replace(line.Substring(i), line.Substring(line.Length - 1));
break;
}
if (numberOfCharsToRead > 0 && testWord.Match(line.Substring(i, numberOfCharsToRead)).Success)
{
line = line.Replace(line.Substring(i, numberOfCharsToRead), string.Empty);
}
}
writer.WriteLine(line);
}
}
}
}
示例6: Main
static void Main()
{
const string Keys = @"(?<startKey>^[A-Za-z_]*)(?=\d).*(?<=\d)(?<endKey>[A-Za-z_]*)";
var keysMatcher = new Regex(Keys);
string keysString = Console.ReadLine();
string startKey = keysMatcher.Match(keysString).Groups["startKey"].Value;
string endKey = keysMatcher.Match(keysString).Groups["endKey"].Value;
if (string.IsNullOrEmpty(startKey) || string.IsNullOrEmpty(endKey))
{
Console.WriteLine("<p>A key is missing</p>");
return;
}
string Pattern = string.Format("(?:{0}){1}(?:{2})", startKey, @"(\d*\.?\d+)", endKey);
var numbersMatcher = new Regex(Pattern);
string textString = Console.ReadLine();
var matches = numbersMatcher.Matches(textString);
var numbers = (from Match number in matches select double.Parse(number.Groups[1].Value)).ToList();
if (numbers.Count == 0)
{
Console.WriteLine("<p>The total value is: <em>nothing</em></p>");
}
else
{
double sum = numbers.Sum();
Console.WriteLine("<p>The total value is: <em>{0}</em></p>",sum);
}
}
示例7: Main
static void Main( string[] args )
{
string testString =
"regular expressions are sometimes called regex or regexp";
Console.WriteLine( "The test string is\n \"{0}\"", testString );
Console.Write( "Match 'e' in the test string: " );
// match 'e' in the test string
Regex expression = new Regex( "e" );
Console.WriteLine( expression.Match( testString ) );
Console.Write( "Match every 'e' in the test string: " );
// match 'e' multiple times in the test string
foreach ( var myMatch in expression.Matches( testString ) )
Console.Write( "{0} ", myMatch );
Console.Write( "\nMatch \"regex\" in the test string: " );
// match 'regex' in the test string
foreach ( var myMatch in Regex.Matches( testString, "regex" ) )
Console.Write( "{0} ", myMatch );
Console.Write(
"\nMatch \"regex\" or \"regexp\" using an optional 'p': " );
// use the ? quantifier to include an optional 'p'
foreach ( var myMatch in Regex.Matches( testString, "regexp?" ) )
Console.Write( "{0} ", myMatch );
// use alternation to match either 'cat' or 'hat'
expression = new Regex( "(c|h)at" );
Console.WriteLine(
"\n\"hat cat\" matches {0}, but \"cat hat\" matches {1}",
expression.Match( "hat cat" ), expression.Match( "cat hat" ) );
}
示例8: Main
private static void Main(string[] args)
{
if (args.Length != 2) {
Console.WriteLine("Usage: stacktrace_decoder <info_file_uri> <pdb_file>");
return;
}
string info_file_uri = args[0];
string pdb_file = args[1];
var web_client = new WebClient();
var stream = new StreamReader(web_client.OpenRead(info_file_uri));
var version_regex = new Regex(
@"^I.*\] Principia version " +
@"([0-9]{10}-[A-Za-z]+)-[0-9]+-g([0-9a-f]{40}) built");
Match version_match;
do {
version_match = version_regex.Match(stream.ReadLine());
} while (!version_match.Success);
string tag = version_match.Groups[1].ToString();
string commit = version_match.Groups[2].ToString();
var base_address_regex = new Regex(@"^I.*\] Base address is ([0-9A-F]+)$");
string base_address_string =
base_address_regex.Match(stream.ReadLine()).Groups[1].ToString();
Int64 base_address = Convert.ToInt64(base_address_string, 16);
var stack_regex = new Regex(
@"^\[email protected]\s+[0-9A-F]+\s+\(No symbol\) \[0x([0-9A-F]+)\]");
Match stack_match;
do {
stack_match = stack_regex.Match(stream.ReadLine());
} while (!stack_match.Success);
var file_regex = new Regex(
@"file\s+:\s+.*\\principia\\([a-z_]+)\\([a-z_]+\.[ch]pp)");
var line_regex = new Regex(@"line\s+:\s+([0-9]+)");
for (;
stack_match.Success;
stack_match = stack_regex.Match(stream.ReadLine())) {
Int64 address = Convert.ToInt64(stack_match.Groups[1].ToString(), 16);
Int64 dbh_base_address = 0x1000000;
string rebased_address =
Convert.ToString(address - base_address + dbh_base_address, 16);
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = kDBH;
p.StartInfo.Arguments =
'"' + pdb_file + "\" laddr \"" + rebased_address + '"';
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Match file_match = file_regex.Match(output);
if (file_match.Success) {
string file = file_match.Groups[1].ToString() + '/' +
file_match.Groups[2].ToString();
string line = line_regex.Match(output).Groups[1].ToString();
string url = "https://github.com/mockingbirdnest/Principia/blob/" +
commit + '/' + file + "#L" + line;
Console.WriteLine("[`" + file + ":" + line + "`](" + url + ")");
}
}
}
示例9: ComplementPrefix
public void ComplementPrefix()
{
Regex re = new Regex("(...~foo|^.{0,2})bar.*");
Assert.That(re.Match("foobar crowbar etc").ToString(), Is.EqualTo("rowbar etc"));
Assert.That(re.Match("barrel").ToString(), Is.EqualTo("barrel"));
Assert.That(re.Match("2barrel").ToString(), Is.EqualTo("2barrel"));
Assert.That(re.Match("A barrel").ToString(), Is.EqualTo("A barrel"));
}
示例10: BasicIntersection
public void BasicIntersection()
{
Regex re = new Regex("^(.*a.*&.*b.*)$");
Assert.That(re.Match("abcd").ToString(), Is.EqualTo("abcd"));
Assert.That(re.Match("cbda").ToString(), Is.EqualTo("cbda"));
Assert.False(re.Match("aeiou").IsSuccess);
Assert.False(re.Match("robber").IsSuccess);
}
示例11: AlternatingAtomicsInCapture
public void AlternatingAtomicsInCapture()
{
Regex re = new Regex(@"^(+(?>\w+)|(?>\s+))*$");
Assert.That(re.Match("Now is the time for all good men to come to the aid of the party"
).Groups.Select(g => g.ToString()),
Is.EqualTo(new string[] { "Now is the time for all good men to come to the aid of the party", "party" }));
Assert.False(re.Match("this is not a line with only words and spaces!").IsSuccess);
}
示例12: AnchoredRegex
public void AnchoredRegex()
{
Regex re = new Regex(@"fox", RegexOptions.Anchored);
string text = "The quick brown fox jumped over the lazy dog.";
Assert.False(re.Match(text).IsSuccess, "{0} matched \"{1}\"", re.ToString(), text);
Assert.True(re.Match(text, 16).IsSuccess,
"{0} did not match \"{1}\" at {2:d}", re.ToString(), text, 16);
}
示例13: CaseFoldModeSwitch
public void CaseFoldModeSwitch()
{
Regex re = new Regex("the (?i)quick brown(?-i) fox", RegexOptions.IgnoreCase);
Assert.AreEqual("the quick brown fox", re.Match("the quick brown fox").ToString());
Assert.AreEqual("the QUICK BROWN fox", re.Match("the QUICK BROWN fox").ToString());
Assert.AreEqual("the QUICK brown fox", re.Match("the QUICK brown fox").ToString());
Assert.False(re.Match("the quick brown FOX").IsSuccess);
}
示例14: Load
// Loads the Reads the data in the ini file into the IniFile object
public bool Load(string sFileName, bool bMerge )
{
bool bRet = true;
if (!bMerge)
{
RemoveAllSections();
}
if (File.Exists(sFileName) == false)
{
return(false);
}
// Clear the object...
IniSection tempsection = null;
StreamReader oReader = new StreamReader(sFileName);
Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
// ^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$
Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
//Regex regexsection = new Regex("\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\]", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
Regex regexkey = new Regex("^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase));
while (!oReader.EndOfStream)
{
string line = oReader.ReadLine();
if (line != string.Empty)
{
Match m = null;
if (regexcomment.Match(line).Success)
{
m = regexcomment.Match(line);
Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value));
}
else if (regexsection.Match(line).Success)
{
m = regexsection.Match(line);
Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value));
tempsection = AddSection(m.Groups[1].Value);
}
else if ( regexkey.Match(line).Success && tempsection != null)
{
m = regexkey.Match(line);
Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value));
tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value;
}
else if ( tempsection != null )
{
// Handle Key without value
Trace.WriteLine(string.Format("Adding Key [{0}]", line));
tempsection.AddKey(line);
}
else
{
// This should not occur unless the tempsection is not created yet...
Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line));
}
}
}
oReader.Close();
return bRet;
}
示例15: BasicBackref
public void BasicBackref()
{
Regex re = new Regex(@"\b{A}(+abc|def)=(+\1){2,3}\b{Z}");
Assert.That(re.Match("abc=abcabc").Groups.Select(g => g.ToString()),
Is.EqualTo(new string[] { "abc=abcabc", "abc", "abc" }));
Assert.That(re.Match("def=defdefdef").Groups.Select(g => g.ToString()),
Is.EqualTo(new string[] { "def=defdefdef", "def", "def" }));
Assert.False(re.Match("abc=defdef").IsSuccess);
}