0

I need a regex to match something like

"4f0f30500be4443126002034"

and

"4f0f30500be4443126002034>4f0f31310be4443126005578"

but not like

"4f0f30500be4443126002034>4f0f31310be4443126005578>4f0f31310be4443126005579"

3
  • Can you provide some more details about what should and shouldn't match? Why does the 3rd one not match? Because it consists of 3 numbers? Commented Jan 13, 2012 at 17:06
  • Yes. I want to only match the string of two numbers, not three. Commented Jan 13, 2012 at 17:08
  • 1
    Well, the string of one or two numbers, not three. To be specific. Commented Jan 13, 2012 at 17:09

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.