jeudi 13 avril 2017

Ruby regexp - test string begins with a specific string

Here is a typical string i want to test:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAsICAoIBwsKCQoNDAsNERwSEQ8PESIZGhQcKSQrKigkJyctMkA3LTA9MCcnOEw5PUNFSElIKzZPVU5GVEBHSEX/2wBDAQwNDREPESESEiFFLicuRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUX/wAARCAAKAB4DAREAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAABgUH/8QAIxAAAgEEAQQDAQAAAAAAAAAAAQIDAAQFERMhIjGBBhIyQf/EABYBAQEBAAAAAAAAAAAAAAAAAAQCA//EACARAAICAgEFAQAAAAAAAAAAAAABAhEEIQMSEyIxYdH/2gAMAwEAAhEDEQA/AM4s7JWhdpEil5YyIzz/AFMTAjqR/em+lWkQyjjLQRXHG/1JU62DsGncS0D5XsXY2aHG/IYXkZIxLZyJCza1ye/HSiZqeqE4LW0w1lbVLbJXADhllIlCkdy7359AH3UcEvGjTIjUrDEX6q4kSL+L/a06HoDP2XMuo5sC2hs3DAnXnujoOS31tfP0bjJdtP6G5GaTMXpdixMr7JO991HTqKoS1cnZ/9k=

I am using validates_format with to check that a string begins with the following characters:

data:image/jpeg;base64

For the moment my code is but it's not working:

validates_format_of :imagebase64,  :with => %r{ \A(data:image/jpeg;base64) }i, :message => "is a invalid data uri base64 file"

Also how can I add something to the regexp so that it rejects if the string has any white space inside the string ?

Aucun commentaire:

Enregistrer un commentaire