Ruby, True/false regex -


so i've got issue regex looks this: /true|false/.

when check word falsee true regex, there way limit exact true or false words?

use regex:

/^(true|false)$/ 

it match beginning , end of test string ^ , $, respectively, nothing else can in string (exact match).

see live example @ regex101.

update (see @w0lf's comment): parentheses isolate true|false clause not grouped incorrectly. (this puts true or false match in first capturing group, since seems matching , not capturing output, should not make difference).


alternatively, if want match 2 values, there easier ways in ruby. @simonecarletti suggests one. can use basic == or eql? operators. try running following script see these work:

values = ["true", "false", "almosttrue", "falsealmost"] values.each | value |   puts value    # these 3 equivalent   puts "match if" if value == "true" || value == "false"   puts "match equals?" if (value.eql? "true") || (value.eql? "false")   puts "match regex" if /^(true|false)$/.match value    puts end 

Comments