WinRegex.xaml.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace Test.RegexTest
  15. {
  16. /// <summary>
  17. /// WinRegex.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class WinRegex : Window
  20. {
  21. public WinRegex()
  22. {
  23. InitializeComponent();
  24. var s1 = System.IO.Path.GetPathRoot(@"c:\");
  25. var s2 = System.IO.Path.GetPathRoot(@"ccc");
  26. var flag1=System.IO.Path.IsPathRooted(@"c:\");
  27. var flag2=System.IO.Path.IsPathRooted(@".\ccc");
  28. }
  29. private void Button_Click(object sender, RoutedEventArgs e)
  30. {
  31. Output.Document.Blocks.Clear();
  32. var input = Input.Text;
  33. if (string.IsNullOrWhiteSpace(input))
  34. return;
  35. var regex = Regex.Text;
  36. if (string.IsNullOrWhiteSpace(regex))
  37. return;
  38. var match=System.Text.RegularExpressions.Regex.Match(input, regex);
  39. if (!match.Success)
  40. {
  41. WriteResult(false);
  42. return;
  43. }
  44. WriteValue(match.Value);
  45. for (int i = 0; i < match.Groups.Count; i++)
  46. {
  47. WriteGroup(i, match.Groups[i].ToString());
  48. }
  49. }
  50. private void WriteOutput(string str)
  51. {
  52. Output.AppendText(Environment.NewLine);
  53. Output.AppendText(str);
  54. }
  55. public void WriteResult(bool isSuccess)
  56. {
  57. WriteOutput(string.Format("Result:{0}", isSuccess.ToString()));
  58. }
  59. public void WriteValue(string value)
  60. {
  61. WriteOutput(string.Format("Value:{0}", value));
  62. }
  63. public void WriteGroup(int index, string val)
  64. {
  65. WriteOutput(string.Format("Group--{0}:{1}", index, val));
  66. }
  67. }
  68. }