Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在RichTextBox中检测不同的匹配_C#_.net_Regex_Windows Phone 8_Windows Phone - Fatal编程技术网

C# 在RichTextBox中检测不同的匹配

C# 在RichTextBox中检测不同的匹配,c#,.net,regex,windows-phone-8,windows-phone,C#,.net,Regex,Windows Phone 8,Windows Phone,我使用以下代码创建了一个新的RichTextBox,用于检测URL: using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Text.RegularExpressions; using System.Windows.Media; namespace NazarGrynko.UI.Controls { public cl

我使用以下代码创建了一个新的RichTextBox,用于检测URL:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace NazarGrynko.UI.Controls
{
    public class MyRichTextBox : RichTextBox
    {
        private const string UrlPattern = @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof (string), typeof(MyRichTextBox ), new PropertyMetadata(default(string), TextPropertyChanged));

        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        private static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var richTextBox = (MyRichTextBox)dependencyObject;
            var text = (string) dependencyPropertyChangedEventArgs.NewValue;
            int textPosition = 0;
            var paragraph = new Paragraph();

            var urlMatches = Regex.Matches(text, UrlPattern);
            foreach (Match urlMatch in urlMatches)
            {
                int urlOccurrenceIndex = text.IndexOf(urlMatch.Value, textPosition, StringComparison.Ordinal);

                if (urlOccurrenceIndex == 0)
                {
                    var hyperlink = new Hyperlink
                                        {
                                            NavigateUri = new Uri(urlMatch.Value),
                                            TargetName = "_blank",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                        };
                    hyperlink.Inlines.Add(urlMatch.Value);
                    paragraph.Inlines.Add(hyperlink);
                    textPosition += urlMatch.Value.Length;
                }
                else
                {
                    paragraph.Inlines.Add(text.Substring(textPosition, urlOccurrenceIndex - textPosition));
                    textPosition += urlOccurrenceIndex - textPosition;
                    var hyperlink = new Hyperlink
                                        {
                                            NavigateUri = new Uri(urlMatch.Value),
                                            TargetName = "_blank",
                                            Foreground = Application.Current.Resources["PhoneAccentBrush"] as Brush
                                        };
                    hyperlink.Inlines.Add(urlMatch.Value);
                    paragraph.Inlines.Add(hyperlink);
                    textPosition += urlMatch.Value.Length;
                }
            }

            if (urlMatches.Count == 0)
            {
                paragraph.Inlines.Add(text);
            }

            richTextBox.Blocks.Add(paragraph);
        }
    }
}
我想进行编辑,以便它也能识别其他字符串

我添加了这个模式:

    const string dollarPattern = @"(?<=\s?)[$]\w+(?=\s?)";

conststringdollarpattern=@”(?使用带
(?=\s?)
(?conststringdollarpattern=@“[$]\w+”)的环顾断言,像这样?