简单的C#Regex

时间:2009-05-07 14:08:36

标签: c# regex

我正在尝试从Web服务响应中对字符串中给出的哈希进行regex-out。给出的答复格式为:

Write this code down, this is your id for the following tasks: P+3zLI8x1AokfbZ1jXat9g==

You will need to enter the code in each of the following steps.

The next task is somewhat simpler, and demonstrates the process of validating data input to a service.

The next method that has been exposed for you is a method called 'girls'.  
This method takes five parameters, the first four of which are the first names of the members of Girls Aloud, in alphabetical order (a quick search on wikipedia will help out here), and the fifth parameter is your unique id code.  

Create a windows application to send the correct data and the response will be a string with your next set of instructions.`

我感兴趣的哈希是“id”,id est,"P+3zLI8x1AokfbZ1jXat9g=="。我尝试使用像"^:\\w*=$"这样的正则表达式,但它与它不匹配......

有人可以帮我一把吗?

(并且,对于那些感兴趣的人,它来自非cs课程的简单web服务示例;我正在尝试通过正则表达式提取哈希而不是仅仅将其写下来。)

5 个答案:

答案 0 :(得分:1)

  

[A-ZA-Z + 0-9] {22} ==

应该有效,因为哈希值始终是特定长度并且使用有限的字符集(没有标点符号)。

您发布的正则表达式的最大问题是您尝试将其锚定到行的前端和末尾,这样如果散列是该行上的唯一事物,则表达式将匹配。你真的不需要这样做。如果你真的想要,你可以将这个表达式锚定到该行的 end ,但没有必要得到你的匹配。

答案 1 :(得分:1)

也许尝试阅读本文的第一行,然后将其拆分为

string hash = stream.ReadLine().Split(":")[1];

答案 2 :(得分:1)

tasks:\s(.+)\W

答案 3 :(得分:1)

\ S * ==

这匹配一行末尾的所有整个单词,以'=='结尾。

RegexStudio是你的朋友;)

答案 4 :(得分:0)

string resultString = null;
try {
    resultString = Regex.Match(subjectString, "^Write this code down, this is your id for the following tasks: (?<ID>.+)$", RegexOptions.Multiline).Groups["ID"].Value;
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}