@@ -309,7 +309,7 @@ i*(t+9-g)/8,4-a+b
309
309
```
310
310
311
311
* ` index ` method returns matching position (starts at 0) and nil if not found
312
- * supports both string and regex
312
+ * supports both string and regexp
313
313
* optional 2nd argument allows to specify offset to start searching
314
314
* See [ ruby-doc: index] ( https://ruby-doc.org/core-2.5.0/String.html#method-i-index ) for details
315
315
@@ -321,7 +321,7 @@ i*(t+9-g)/8,4-a+b
321
321
$ ruby -ne ' print if $_.index("a+b")==0' eqns.txt
322
322
a+b,pi=3.14,5e12
323
323
324
- $ # passing regex
324
+ $ # passing regexp
325
325
$ ruby -ne ' print if $_.index(/[+*]/)<5' eqns.txt
326
326
a+b,pi=3.14,5e12
327
327
i* (t+9-g)/8,4-a+b
924
924
## <a name =" ruby-regular-expressions " ></a >Ruby regular expressions
925
925
926
926
* assuming that you are already familiar with basics of regular expressions
927
- * if not, see [ Ruby Regular Expressions tutorial ] ( https://github .com/learnbyexample/Ruby_Scripting/blob/master/chapters/Regular_expressions.md )
927
+ * if not, check out [ Ruby Regexp ] ( https://leanpub .com/rubyregexp ) ebook - step by step guide from beginner to advanced levels
928
928
* examples/descriptions based only on ASCII encoding
929
929
* See [ ruby-doc: Regexp] ( https://ruby-doc.org/core-2.5.0/Regexp.html ) for syntax and feature details
930
- * See [ rexegg ruby] ( https://www.rexegg.com/regex-ruby.html ) for a bit of ruby regex history and differences with other regex engines
930
+ * See [ rexegg ruby] ( https://www.rexegg.com/regex-ruby.html ) for a bit of ruby regexp history and differences with other regexp engines
931
931
932
932
<br >
933
933
@@ -1397,7 +1397,7 @@ $ s='a+b' ruby -ne 'print if /#{Regexp.escape(ENV["s"])}/' eqns.txt
1397
1397
a+b,pi= 3.14,5e12
1398
1398
i* (t+9-g)/8,4-a+b
1399
1399
1400
- $ # use regex as needed around variable content, for ex: end of line anchor
1400
+ $ # use regexp as needed around variable content, for ex: end of line anchor
1401
1401
$ ruby -p e ' BEGIN{s="a+b"}; sub(/#{Regexp.escape(s)}$/, "a**b")' eqns.txt
1402
1402
a= b,a-b= c,c* d
1403
1403
a+b,pi= 3.14,5e12
@@ -2153,15 +2153,30 @@ $ ruby -e 'books=%w[Elantris Martian Dune Alchemist]
2153
2153
2154
2154
# ### <a name="filtering"></a>Filtering
2155
2155
2156
- * based on a condition
2156
+ * based on regexp
2157
2157
2158
- ` ` ` bash
2159
- $ # based on regex matching
2158
+ ` ` ` ruby
2160
2159
$ s= ' foo:123:bar:baz'
2161
- $ echo " $s " | ruby -F: -lane ' print $F.select { |s| s =~ /[a-z]/ } * ":"'
2160
+ $ echo " $s " | ruby -F: -lane ' print $F.grep( /[a-z]/) * ":"'
2162
2161
foo:bar:baz
2163
2162
2163
+ $ words= ' tryst fun glyph pity why'
2164
+ $ echo " $words " | ruby -lane ' puts $F.grep(/[a-g]/)'
2165
+ fun
2166
+ glyph
2167
+
2168
+ $ # grep_v inverts the selection
2169
+ $ echo " $words " | ruby -lane ' puts $F.grep_v(/[aeiou]/)'
2170
+ tryst
2171
+ glyph
2172
+ why
2173
+ ` ` `
2174
+
2175
+ * use ` select` or ` reject` for generic conditions
2176
+
2177
+ ` ` ` bash
2164
2178
$ # to get index instead of matches
2179
+ $ s= ' foo:123:bar:baz'
2165
2180
$ echo " $s " | ruby -F: -lane ' print $F.each_index.select{|i| $F[i] =~ /[a-z]/}'
2166
2181
[0, 2, 3]
2167
2182
@@ -2171,10 +2186,9 @@ $ echo "$s" | ruby -lane 'print $F.select { |s| s.to_i < 100 } * " "'
2171
2186
23 -983 5
2172
2187
2173
2188
$ # filters only those elements with successful substitution
2189
+ $ # for opposite, either use negated condition or use reject instead of select
2174
2190
$ echo " $s " | ruby -lane ' print $F.select { |s| s.sub!(/3/, "E") } * " "'
2175
2191
2E -98E
2176
-
2177
- $ # for opposite, either use negated condition or use reject instead of select
2178
2192
` ` `
2179
2193
2180
2194
* random element(s)
@@ -2423,7 +2437,7 @@ $ # specify a negative count to preserve trailing empty fields
2423
2437
$ echo ' :123::' | ruby -lne ' print $_.split(/:/, -1) * ","'
2424
2438
,123,,
2425
2439
2426
- $ # use string argument for fixed-string split instead of regex
2440
+ $ # use string argument for fixed-string split instead of regexp
2427
2441
$ echo ' foo**123**baz' | ruby -lne ' print $_.split("**") * ":"'
2428
2442
foo:123:baz
2429
2443
0 commit comments