为大字符串定义正则表达式

时间:2015-11-10 17:07:58

标签: c# regex

我搜索了Google,但仍然不知道如何为此编写正则表达式:

  

REC-1-20151103142759-d9fd57bc-4fe3-4b91-A640-ddd54ef081dd-扬声器-Andreea.Dumitru-ANDREEADUM-DSK-9fa377fe-3290-4cb7-9c7e-6ea86e01e08f-1066

我必须从这个字符串中获得以下内容:

  • id = 1
  • datetime = 20151103142759
  • guidid2 = d9fd57bc-4fe3-4b91-a640-ddd54ef081dd
  • type = Speakers
  • userName = Andreea.Dumitru
  • sourceName = ANDREEADUM-DSK
  • guidId2 = 9fa377fe-3290-4cb7-9c7e-6ea86e01e08f-1066

我已经开始了

Regex regex = new Regex(-[0-9]{1}(| |-)[0-9]{14}(| |-)[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(| |-)[a-fA-F](| |-)[a-fA-F](| |-) .... 

1 个答案:

答案 0 :(得分:3)

这是一个解决方案:

\w{3}-(\d)-(\d{14})-([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})-(\w+)-([\w.]+)-(\w+-\w+)-([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12}-[\w\d]{4})

这将是C#中没有正则表达式选项的行:

Regex regex = new Regex(@"\w{3}-(\d)-(\d{14})-([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12})-(\w+)-([\w.]+)-(\w+-\w+)-([\w\d]{8}-[\w\d]{4}-[\w\d]{4}-[\w\d]{4}-[\w\d]{12}-[\w\d]{4})");

这是regex101:https://regex101.com/r/bI8yH6/1