62

With reference to this SO question, I have a scenario where I only need to match a hex string with a-f included. All else should not match. Example:

checkForHexRegExp.test("112345679065574883030833"); // => false
checkForHexRegExp.test("FFFFFFFFFFFFFFFFFFFFFFFF"); // => false
checkForHexRegExp.test("45cbc4a0e4123f6920000002"); // => true

My use case is that I am working with a set of hex strings and would like to only validate as true those that are mongodb objectIDs.

5
  • 3
    112345679065574883030833, FFFFFFFFFFFFFFFFFFFFFFFF are also valid hexadecimal representations. Why false for those strings? Commented Jan 8, 2014 at 6:36
  • @falsetru As per my use case, I need a regex that will only match mongodb ObjectIDs. Commented Jan 8, 2014 at 6:38
  • 2
    As answerd by @olegshparber all the above IDs form a valid mongodb object ID please refer to mongodb Object ID documentation Commented Jan 8, 2014 at 6:58
  • @gabeno I still don't understand what you're trying to do as all three are valid MongoDB ObjectIDs. Commented Jan 8, 2014 at 13:39
  • 1
    He wants to match MongoDB ObjectIDs in practice, not in theory. Commented Jun 2, 2015 at 2:27

5 Answers 5

116

You can use following regular expression but it will not quite work

checkForHexRegExp = /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i

Example:

> checkForHexRegExp.test("112345679065574883030833")
false
> checkForHexRegExp.test("FFFFFFFFFFFFFFFFFFFFFFFF")
false
> checkForHexRegExp.test("45cbc4a0e4123f6920000002")
true

But, as I commented, 112345679065574883030833, FFFFFFFFFFFFFFFFFFFFFFFF are also valid hexadecimal representations.

You should use /^[a-f\d]{24}$/i because it passes all the above tests

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

6 Comments

is i flag required?
@transang, Yes it is. (to ignore case)
It could be removed though, but that'd make the regex itself longer /^[a-fA-F\d]{24}$/. Not sure what the performance impact is, but I assume if there is one, it'll be minimal (at least in this case)
ObjectId.TryParse returns true for "FFFFFFFFFFFFFFFFFFFFFFFF"
Sorry, it is a C# method. I just wanted to assert that the Mongo Driver seem to think that it is a valid ObjectId.
|
31

I need a regex that will only match mongodb ObjectIDs

If you need that, you will have to specify exactly what makes up a mongodb ObjectID, so that we can create the appropriate regex string for it.


This should technically work in js:

var myregexp = /^[0-9a-fA-F]{24}$/;
subject = "112345679065574883030833";

if (subject.match(myregexp)) {
    // Successful match
} else {
    // Match attempt failed
}

Comments

5

Technically, all examples in the question potentially could be valid ObjectIds. If you have to add some additional verification and that regexp is not enough, then my suggestion is to check if the first 4 bytes are a valid timestamp. You can even verify that ObjectId has been generated during a certain period of time (e.g. since your project has been started or so). See ObjectId documentation for details.

If there's another timestamp field in an object, then it's also possible to make sure that both times are really close.

Just for the reference, in MongoDB shell ObjectId::getTimestamp() method can be used to extract a timestamp from ObjectId.

Comments

3

I would do something like this

function validateObjectId(id)
{
    var bool=false; 
    if(id.length==24) bool=/[a-f]+/.test(id);
    return bool;
}


> validateObjectId("112345679065574883030833")
  false
> validateObjectId("FFFFFFFFFFFFFFFFFFFFFFFF")
  false
> validateObjectId("45cbc4a0e4123f6920000002")
  true
> validateObjectId("45cbc4a0e4123f6920")
  false

1 Comment

validateObjectId("ffffffffffffffffffffffff") returns true
0

For those who was looking just for hex checking the string:

s := "5fa0ef460c2056137465a39b"
if _, err := hex.DecodeString(s); err == nil {
    fmt.Println("ok")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.