Regular Expressions
Don't use regular expressions if you just need plain text search in string:
string["text"][link]For simple constructions you can use regexp directly through string index. [link]
match = string[/regexp/] # get content of matched regexp first_group = string[/text(grp)/, 1] # get content of captured group string[/text (grp)/, 1] = "replace" # string => "text replace"Use non-capturing groups when you don't use the captured result. [link]
# bad /(first|second)/ # good /(?:first|second)/Avoid using numbered groups as it can be hard to track what they contain. Named groups can be used instead. [link]
# bad /(regexp)/ =~ string ... process Regexp.last_match(1) # good /(?<meaningful_var>regexp)/ =~ string ... process meaningful_varCharacter classes have only a few special characters you should care about:
^,-,\,], so don't escape.or brackets in[]. [link]Be careful with
^and$as they match start/end of line, not string endings. If you want to match the whole string use:\Aand\z(not to be confused with\Zwhich is the equivalent of/\n?\z/). [link]string = "some injection\nusername" string[/^username$/] # matches string[/\Ausername\z/] # doesn't matchUse
xmodifier for complex regexps. This makes them more readable and you can add some useful comments. Just be careful as spaces are ignored. [link]regexp = / start # some text \s # white space char (group) # first group (?:alt1|alt2) # some alternation end /xFor complex replacements
sub/gsubcan be used with a block or a hash. [link]words = "foo bar" words.sub(/f/, "f" => "F") # => "Foo bar" words.gsub(/\w+/) { |word| word.capitalize } # => "Foo Bar"