1

How do we represent a string in ruby like for integer we do \d, is it \s in ruby?

I tried \s and \c*, but nothing worked for me. I have a constraint in my file as per below:

constraints: {:name => /\s+/}

but its not accepting \s for some reason. For integer I use constraints: {:id => /\d+/} and it works.

1
  • 2
    Arbitrary string? Or are there any constraints on the string at all, like must be alphanumeric? Commented Nov 22, 2013 at 23:44

1 Answer 1

1

For word characters you would use:

constraints: {:name => /\w+/}

\s is whitespace, so it wouldn't mark words.

you could also use:

constraints: {:name => /\S+/}

This will check for any NON-whitespace chars.

It depends on what exactly you're wanting to match.

http://rubular.com/ (contains a list with its practice box)

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

4 Comments

You might want to see what 'é' =~ /\w+/ is.
Thanks for your replay and sharing the link. I am using a string which will contain words but can also contain white spaces in between words. Will try the link you gave to see if it can handle both words and space, else might will have to think of handling spaces separately.
For example :name in my case could be "This is a name"
@tech_learning It seems like you want to match everything… What don’t you want to match?

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.