user1448020
user1448020

Reputation: 157

Regex to match bitmessage address

I'm having difficulty coming up with valid regex to find all occurrences of bitmessage addresses within a string of text.

An example of a bitmessage address is: BM-BcbRqcFFSQUUmXFKsPJgVQPSiFA3Xash

The valid characters after the BM- part are 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ (missing I l O 0)

The documentation says address part after the 'BM-' prefix is between 32-34 chars in length.

I'm not sure if this can be done with regex, i'm having difficulty getting it to match at a specific length, with the valid a-z minus the excluded characters.

I am planning on implementing the regex using PHP if that makes a difference to the regex string.

Sources:

charset: https://bitmessage.org/forum/index.php?topic=3255.0

address length: https://bitmessage.org/wiki/Address#Length

Upvotes: 0

Views: 225

Answers (3)

walid toumi
walid toumi

Reputation: 2282

Another variant much faster no lookaround to test every character

/^BM-[1-9a-km-zA-HJ-NP-Z]{32,34}$/

Upvotes: 0

taco
taco

Reputation: 1383

In PHP, you can use preg_match() function.

$subject = "BM-BcbRqcFFSQUUmXFKsPJgVQPSiFA3Xash";
$pattern = '/BM-[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{32,34}/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 0);
print_r($matches);

This is the simplified, newbie regex. It could be replaced with a more concise pattern.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174776

This could be easily possible through regex,

^BM-(?:(?![IlO0])[A-Za-z0-9]){32,34}$

DEMO

(?:(?![IlO0])[A-Za-z0-9]){32,34} tells the regex engine that, Match any character from the alphanumeric set [A-Za-z0-9] except for I or l or O or 0. And also the character length must be from 32 to 34.

Remove the start and end anchors if this Bit message address is located at the middle or in some part of a line.

Upvotes: 1

Related Questions