Regular expression help (again...)

Status
Not open for further replies.
Looking to check whether a 32 character (hash) is present in a string.

E.g. string is "Reply - Call [44294]-[4B2B8B9877D5EC00E341D08183BF311A]"

making use of the bounding [ and ] brackets, I'm looking to check whether there is a 32 character (a-f,0-9) substring in there.

This is for an automated reply to email to filter out non genuine replies. The hash is based off a lookup value associated with the first number.

Thanks!
 
Solomonoff's Secret":13zzo8zw said:
Those brackets match the hash itself. If you just care about whether the hash exists, you should remove need them, but if you want to extract the hash, for example to validate it, then you should use the parentheses to grab the hash from the matched text.

Ahh, yep, can see that now. I was getting the position in the string by adding one to the start location and taking two off the length to remove the brackets but can see in my return values that the second set are exactly that, i.e. just the values I need.

Thanks!
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Ahh, yep, can see that now. I was getting the position in the string by adding one to the start location and taking two off the length to remove the brackets but can see in my return values that the second set are exactly that, i.e. just the values I need.
If you want to "simplify" things (ha!) you could use a look-around assertion to return the correct result. Not sure if ColdFusion has them, though.
Code:
$ perl
my $string = "Reply - Call [44294]-[4B2B8B9877D5EC00E341D08183BF311A]";
my ($result) = ($string =~ /(?<=\[)([a-f0-9]{32})(?=\])/i);
print "result = '$result'\n";
result = '4B2B8B9877D5EC00E341D08183BF311A'
It's ugly. Hence the quotes around "simplify". It probably looks a little better like this:
Code:
my ($result) = ($string =~ m{
        (?<=\[)
        ([a-f0-9]{32})
        (?=\])
    }ix);
 
Status
Not open for further replies.