@@ -990,6 +990,67 @@ syntax.
990990 end
991991 ```
992992
993+ ## Regular Expressions
994+
995+ * Don' t use regular expressions if you just need plain text search in string:
996+ ` string['text']`
997+ * For simple constructions you can use regexp directly through string index.
998+
999+ ` ` ` Ruby
1000+ match = string[/regexp/] # get content of matched regexp
1001+ first_group = string[/text(grp)/, 1] # get content of captured group
1002+ string[/text (grp)/, 1] = 'replace' # string => 'text replace'
1003+ ` ` `
1004+
1005+ * Use non capturing groups when you don' t use captured result of parenthesis.
1006+
1007+ ```Ruby
1008+ /(first|second)/ # bad
1009+ /(?:first|second)/ # good
1010+ ```
1011+
1012+ * Avoid using $1-9 as it can be hard to track what they contain. Named groups
1013+ can be used instead.
1014+
1015+ ```Ruby
1016+ # bad
1017+ /(regexp)/ =~ string
1018+ ...
1019+ process $1
1020+
1021+ # good
1022+ /(?<meaningful_var>regexp)/ =~ string
1023+ ...
1024+ process meaningful_var
1025+ ```
1026+
1027+ * Character classes have only few special characters you should care about:
1028+ `^`, `-`, `\`, `]`, so don' t escape ` .` or brackets in ` []` .
1029+
1030+ * Be careful with ` ^` and ` $` as they match start/ end of line, not string endings.
1031+ If you want to match the whole string use: ` \A ` and ` \Z ` .
1032+
1033+ ` ` ` Ruby
1034+ string = "some injection\n username"
1035+ string[/^username$/] # matches
1036+ string[/\A username\Z /] # don't match
1037+ ` ` `
1038+
1039+ * Use ` x` modifier for complex regexps. This makes them more readable and you
1040+ can add some useful comments. Just be careful as spaces are ignored.
1041+
1042+ ` ` ` Ruby
1043+ regexp = %r{
1044+ start # some text
1045+ \s # white space char
1046+ (group) # first group
1047+ (?:alt1|alt2) # some alternation
1048+ end
1049+ }x
1050+ ` ` `
1051+
1052+ * For complex replacements ` sub` / ` gsub` can be used with block or hash.
1053+
9931054# # Percent Literals
9941055
9951056* Use ` %w` freely.
0 commit comments