Skip to content

Commit 232e98d

Browse files
committed
Merge pull request #81 from vsakarov/master
added regular expression section
2 parents e31e80d + e026a7c commit 232e98d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nusername"
1035+
string[/^username$/] # matches
1036+
string[/\Ausername\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

Comments
 (0)