简体   繁体   English

在正则表达式中匹配字符串字符

[英]Matching string characters in regex

How do we represent a string in ruby like for integer we do \\d, is it \\s in ruby? 我们如何用红宝石表示一个字符串,就像我们用\\ d表示整数一样,用红宝石表示\\ s?

I tried \\s and \\c*, but nothing worked for me. 我尝试了\\ s和\\ c *,但是没有任何效果。 I have a constraint in my file as per below: 我的文件中有一个约束,如下所示:

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

but its not accepting \\s for some reason. 但由于某种原因它不接受\\ s。 For integer I use constraints: {:id => /\\d+/} and it works. 对于整数,我使用constraints: {:id => /\\d+/}并且它起作用。

For word characters you would use: 对于文字字符,您可以使用:

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

\\s is whitespace, so it wouldn't mark words. \\s是空格,因此不会标记单词。

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) http://rubular.com/ (包含一个列表及其练习框)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM