本文整理汇总了C#中Regex.Matches方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.Matches方法的具体用法?C# Regex.Matches怎么用?C# Regex.Matches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Regex
的用法示例。
在下文中一共展示了Regex.Matches方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveTags
public void SaveTags(int ObjectID)
{
string strTags = txtTags.Value;
Regex rex = new Regex("\\{(.*?)\\}");
BSTerm.RemoveTo(TermTypes.Tag, ObjectID);
foreach (Match item in rex.Matches(strTags))
{
Regex rx = new Regex("'(.*?)'");
string strText = rx.Matches(item.Value)[1].Value;
string strValue = rx.Matches(item.Value)[0].Value;
strText = strText.Substring(1, strText.Length - 2);
strValue = strValue.Substring(1, strValue.Length - 2);
string code = BSHelper.CreateCode(strText);
BSTerm bsTerm = BSTerm.GetTerm(code, TermTypes.Tag);
if (bsTerm == null)
{
bsTerm = new BSTerm();
bsTerm.Name = strText;
bsTerm.Code = code;
bsTerm.Type = TermTypes.Tag;
bsTerm.Objects.Add(ObjectID);
bsTerm.Save();
}
else
{
bsTerm.Objects.Add(ObjectID);
bsTerm.Save();
}
}
}
示例2: Main
public static void Main()
{
StreamReader wordReader = new StreamReader(@"..\..\words.txt");
StreamWriter resultWriter = new StreamWriter(@"..\..\result.txt");
using (wordReader)
{
using (resultWriter)
{
while (true)
{
//Iterate all of words from file and searching for match with regex
string currentWord = wordReader.ReadLine();
if (currentWord == null)
{
break;
}
currentWord = currentWord.ToLower();
string text = File.ReadAllText(@"..\..\text.txt").ToLower();
Regex regex = new Regex(@"\b" + currentWord + @"\b");
resultWriter.WriteLine("{0} - {1}", currentWord, regex.Matches(text).Count);
Console.WriteLine("{0} - {1}", currentWord, regex.Matches(text).Count);
}
Console.WriteLine("The results was saved in file result.txt in project directory !");
}
}
}
示例3: AllMatches
public void AllMatches()
{
Regex re = new Regex(@"\w+");
string text = "The quick brown fox jumped over the lazy dog.";
Assert.That(re.Matches(text).Select(m => m.ToString()), Is.EqualTo(
new string[] { "The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }));
Assert.That(re.Matches(text, 19).Select(m => m.ToString()), Is.EqualTo(
new string[] { "jumped", "over", "the", "lazy", "dog" }));
}
示例4: GetImgTag
/// <summary>
/// 获取图片标志
/// </summary>
private string[] GetImgTag(string htmlStr)
{
Regex regObj = new Regex("<img.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] strAry = new string[regObj.Matches(htmlStr).Count];
int i = 0;
foreach (Match matchItem in regObj.Matches(htmlStr))
{
strAry[i] = GetImgUrl(matchItem.Value);
i++;
}
return strAry;
}
示例5: Main
public static void Main()
{
while (true)
{
string[] input = Console.ReadLine().Split(' ');
if (input[0] == "END")
{
break;
}
for (int i = 0; i < input.Length; i++)
{
string pattern = @"\b(?:\d*|\W*)([A-Z]{1,})(?:\d*|\W*)\b";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(input[i]);
foreach (Match match in matches)
{
string oldString = match.Groups[1].ToString();
string reverse = new string(oldString.Reverse().ToArray());
if (match.Groups[1].Length == 1 || oldString.Equals(reverse))
{
string newString = ReplaceWithDoubleLetters(oldString);
input[i] = input[i].Replace(oldString, newString);
}
else
{
input[i] = input[i].Replace(oldString, reverse);
}
}
}
Console.WriteLine(SecurityElement.Escape(string.Join(" ", input)));
}
}
示例6: Main
static void Main()
{
List<int> input = Console.ReadLine().
Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).
Select(int.Parse).
ToList();
string line = Console.ReadLine();
while (line != "end")
{
string pattern = @"\d";
Regex regex = new Regex(pattern);
MatchCollection match = regex.Matches(line);
for (int start = int.Parse(match[0].Value), end = int.Parse(match[0].Value) + int.Parse(match[1].Value);
start <= int.Parse(match[0].Value) + int.Parse(match[1].Value);
start++, end--)
{
int tempNum = input[start];
input[start] = input[end];
input[end] = tempNum;
}
line = Console.ReadLine();
}
}
示例7: ReadInput
private static State ReadInput()
{
var contains = new Regex("(?:a ([a-z]+)-compatible|([a-z]+) generator)");
var microchipFloors = new Dictionary<string, int>();
var generatorFloors = new Dictionary<string, int>();
for (int i = 0; i < 4; i++) {
string line = Console.ReadLine();
foreach (Match match in contains.Matches(line)) {
if (match.Groups[1].Success) {
microchipFloors.Add(match.Groups[1].ToString(), i);
} else {
generatorFloors.Add(match.Groups[2].ToString(), i);
}
}
}
var microchipLocations = new int[microchipFloors.Count];
var generatorLocations = new int[generatorFloors.Count];
int j = 0;
foreach (string element in microchipFloors.Keys) {
microchipLocations[j] = microchipFloors[element];
generatorLocations[j] = generatorFloors[element];
j++;
}
return new State(0, microchipLocations, generatorLocations);
}
示例8: Main
static void Main()
{
StringBuilder line = new StringBuilder();
string pattern = @"(?<![a-zA-Z])[A-Z]+(?![a-zA-Z])";
Regex regex = new Regex(pattern);
while (line.ToString() != "END")
{
MatchCollection matches = regex.Matches(line.ToString());
int offset = 0;
foreach (Match match in matches)
{
string word = match.Value;
string reversed = Reverse(word);
if (reversed == word)
{
reversed = DoubleEachLetter(reversed);
}
int index = match.Index;
line = line.Remove(index + offset, word.Length);
line = line.Insert(index + offset, reversed);
offset += reversed.Length - word.Length;
}
Console.WriteLine(SecurityElement.Escape(line.ToString()));
line = new StringBuilder(Console.ReadLine());
}
}
示例9: CountWords
//Write a program that reads a list of words from a file words.txt and finds how many times
//each of the words is contained in another file test.txt. The result should be written in
//the file result.txt and the words should be sorted by the number of their occurrences in
//descending order. Handle all possible exceptions in your methods.
private static Dictionary<string, int> CountWords(string inputPath, string listOfWordsPath)
{
List<string> list = ExtractList(listOfWordsPath);
StreamReader reader = new StreamReader(inputPath);
Dictionary<string, int> result = new Dictionary<string, int>();
using (reader)
{
while (reader.Peek() != -1)
{
string currline = reader.ReadLine();
for (int i = 0; i < list.Count; i++)
{
Regex word = new Regex("\\b" + list[i] + "\\b");
foreach (Match match in word.Matches(currline))
{
//for each word met, if already met - increase counter, else - start counting it
if (result.ContainsKey(list[i]))
{
result[list[i]]++;
}
else
{
result.Add(list[i], 1);
}
}
}
}
}
return result;
}
示例10: Main
static void Main()
{
int n = int.Parse(Console.ReadLine());
string concatenated = "";
for (int i = 0; i < n; i++)
{
int number = int.Parse(Console.ReadLine());
concatenated += Convert.ToString(number, 2).PadLeft(32, '0').Substring(2, 30);//TODO use stringbuilder
}
//Console.WriteLine(concatenated);
Regex reg1 = new Regex(@"(1)\1+");
MatchCollection matches = reg1.Matches(concatenated);
int longest1 = 0;
foreach (System.Text.RegularExpressions.Match match in matches)
{
if (longest1 < match.Length) longest1 = match.Length;
}
Console.WriteLine(longest1);
Regex reg2 = new Regex(@"(0)\1+");
MatchCollection matches2 = reg2.Matches(concatenated);
int longest2 = 0;
foreach (System.Text.RegularExpressions.Match match in matches2)
{
if (longest2 < match.Length) longest2 = match.Length;
}
Console.WriteLine(longest2);
}
示例11: 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);
}
}
示例12: GetParam
public static int GetParam(SqlString s)
{
// 在此处放置代码
Regex _break_comp = new Regex(@"\{\d\}", RegexOptions.Compiled);
return _break_comp.Matches(s.ToString() ).Count;
//return new SqlString("Hello");
}
示例13: Main
static void Main()
{
Console.WriteLine("Please, enter the wanted keyword: ");
string keyword = Console.ReadLine();
List<string> lister = new List<string>();
Console.WriteLine("Enter your text: ");
string text = Console.ReadLine();
string sentencePattern = @"(\S.+?[.!?])(?=\s+|$)";
Regex sentenceRgx = new Regex(sentencePattern);
string keyWordPattern = string.Format(@"\b{0}\b", Regex.Escape(keyword));
MatchCollection matchs = sentenceRgx.Matches(text);
Console.WriteLine();
foreach (var match in matchs)
{
string word = match.ToString();
if (Regex.IsMatch(word, keyWordPattern))
{
Console.WriteLine(match);
}
}
}
示例14: Main
static void Main()
{
const string Pattern = @"([^\d]+(?=\d))((?<=[^\d])[0-9]+)";
var regex = new Regex(Pattern);
string input = Console.ReadLine();
var matches = regex.Matches(input);
var currentResult = new StringBuilder();
var result = new StringBuilder();
foreach (Match match in matches)
{
string currentString = match.Groups[1].Value;
int currentNumber = int.Parse(match.Groups[2].Value);
for (int i = 0; i < currentNumber; i++)
{
currentResult.Append(currentString.ToUpper());
}
result.Append(currentResult);
currentResult.Clear();
}
string count = new string(result.ToString().Distinct().ToArray());
Console.WriteLine("Unique symbols used: {0}",count.Length);
Console.WriteLine(result);
}
示例15: Main
static void Main()
{
string text = "Write a program that reads a string from the console and prints all different letters in the string along with information how many times each letter is found.";
for (char c = 'a'; c <= 'z'; c++)
{
Regex regex = new Regex(c.ToString(), RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
if (matches.Count > 0)
{
Console.WriteLine(c.ToString() + '-' + matches.Count);
}
}
Console.WriteLine("Another solution:");
int[] count = new int['z' - 'a' + 1];
text = text.ToLower();
for (int i = 0; i < text.Length; i++)
{
if (text[i] >= 'a' && text[i] <= 'z')
{
count[text[i] - 'a']++;
}
}
for (int j = 0; j < count.Length; j++)
{
if (count[j] > 0)
{
Console.WriteLine("{0}-{1}", (char)(j + 'a'), count[j]);
}
}
}