Red
Red

Reputation: 2246

Regexp for specific matching of character string

I need a regex to match something like

"4f0f30500be4443126002034"

and

"4f0f30500be4443126002034>4f0f31310be4443126005578"

but not like

"4f0f30500be4443126002034>4f0f31310be4443126005578>4f0f31310be4443126005579"

Upvotes: 0

Views: 158

Answers (3)

slindsey3000
slindsey3000

Reputation: 4271

Don't need a regex.

str = "4f0f30500be4443126002034>4f0f31310be4443126005578"

match = str.count('>') < 2

match will be set to true for matches where there are 1 or 0 '>' in the string. Otherwise match is set to false.

Upvotes: 0

CanSpice
CanSpice

Reputation: 35788

I think you want something like:

/^[0-9a-f]{24}(>[0-9a-f]{24})?$/

That matches 24 characters in the 0-9a-f range (which matches your first string) followed by zero or one strings starting with a >, followed by 24 characters in the 0-9a-f range (which matches your second string). Here's a RegexPal for this regex.

Upvotes: 1

mikej
mikej

Reputation: 66263

Try:

^[\da-f]{24}(>[\da-f]{24})?$

[\da-f]{24} is exactly 24 characters consisting only of 0-9, a-f. The whole pattern is one such number optionally followed by a > and a second such number.

Upvotes: 1

Related Questions