tags:

views:

138

answers:

5

I'm trying to regex-out a hash given in a string from a web-service response. 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==". I tried using a regex such as "^:\\w*=$" but it didn't match it...

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

+1  A: 

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

Joel Coehoorn
it's an md5 hash; do they always end with a "=="? i've recieved several hashes and they appear to do so...
Beau Martínez
It's complicated, but you can generally assume 'yes'.
Joel Coehoorn
+1  A: 

Maybe try reading first line of this text, and then split it like this

string hash = stream.ReadLine().Split(":")[1];
Migol
+1  A: 
tasks:\s(.+)\W
Fredrik Leijon
You were missing the (white) space before.
steamer25
A: 
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
}
Chris Doggett
+1  A: 

\S*==

This matches all whole words at the end of a line, ending with '=='.

RegexStudio is your friend ;)

Peter R
I prefer your answer the most due to it's simplicity. Nice one!
Beau Martínez
You might make that a \S+== Otherwise it would match just a plain "==", which given the context, could easily occur.
patjbs