简体   繁体   English

简单的C#正则表达式

[英]Simple C# Regex

I'm trying to regex-out a hash given in a string from a web-service response. 我正在尝试从Web服务响应中正则表达式中的字符串中给出的哈希值。 The response given is in the format: 给出的响应格式为:

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.`

The hash I'm interested in is the "id", id est, "P+3zLI8x1AokfbZ1jXat9g==" . 我感兴趣的哈希是“ id”,id est, "P+3zLI8x1AokfbZ1jXat9g==" I tried using a regex such as "^:\\\\w*=$" but it didn't match it... 我尝试使用正则表达式,例如"^:\\\\w*=$"但与它不匹配...

Can anyone give me a hand here? 有人可以帮我吗?

(And, for those intrerested, it is from a simple web-services example from a non-cs course; I'm trying to extract the hash via regex instead of just writing it down.) (而且,对于那些精打细算的人来说,它来自非CS课程中的一个简单的Web服务示例;我正尝试通过正则表达式提取哈希值,而不仅仅是写下来。)

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

Should work because hashes are always a specific length and use a limited character set (no punctuation). 应该起作用,因为哈希值始终是特定长度,并且使用了有限的字符集(无标点符号)。

The biggest problem with the regex you posted is that you tried to anchor it to the front and end of the line, such that the expression would match if the hash is the only thing on that line. 您发布的正则表达式的最大问题是,您试图将其锚定到行的开头和结尾,这样,如果哈希是该行中唯一的内容,则表达式将匹配。 You don't really need to do that. 您实际上不需要这样做。 If you really want to, you can anchor this expression to just the end of the line, but it's not necessary to get your match. 如果确实需要,可以将此表达式锚定在行的末尾 ,但是不必进行匹配。

也许尝试阅读此文本的第一行,然后像这样拆分它

string hash = stream.ReadLine().Split(":")[1];
tasks:\s(.+)\W

\\S*== \\ S * ==

This matches all whole words at the end of a line, ending with '=='. 这将匹配所有完整的单词,并以'=='结尾。

RegexStudio is your friend ;) RegexStudio是您的朋友;)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM