Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit 9650a0e

Browse files
regexp book link and grep/grep_v examples
1 parent 5604155 commit 9650a0e

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

‎ruby_one_liners.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ i*(t+9-g)/8,4-a+b
309309
```
310310

311311
* `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
313313
* optional 2nd argument allows to specify offset to start searching
314314
* See [ruby-doc: index](https://ruby-doc.org/core-2.5.0/String.html#method-i-index) for details
315315

@@ -321,7 +321,7 @@ i*(t+9-g)/8,4-a+b
321321
$ ruby -ne 'print if $_.index("a+b")==0' eqns.txt
322322
a+b,pi=3.14,5e12
323323

324-
$ # passing regex
324+
$ # passing regexp
325325
$ ruby -ne 'print if $_.index(/[+*]/)<5' eqns.txt
326326
a+b,pi=3.14,5e12
327327
i*(t+9-g)/8,4-a+b
@@ -924,10 +924,10 @@ a
924924
## <a name="ruby-regular-expressions"></a>Ruby regular expressions
925925

926926
* 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
928928
* examples/descriptions based only on ASCII encoding
929929
* 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
931931

932932
<br>
933933

@@ -1397,7 +1397,7 @@ $ s='a+b' ruby -ne 'print if /#{Regexp.escape(ENV["s"])}/' eqns.txt
13971397
a+b,pi=3.14,5e12
13981398
i*(t+9-g)/8,4-a+b
13991399
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
14011401
$ ruby -pe 'BEGIN{s="a+b"}; sub(/#{Regexp.escape(s)}$/, "a**b")' eqns.txt
14021402
a=b,a-b=c,c*d
14031403
a+b,pi=3.14,5e12
@@ -2153,15 +2153,30 @@ $ ruby -e 'books=%w[Elantris Martian Dune Alchemist]
21532153
21542154
#### <a name="filtering"></a>Filtering
21552155
2156-
* based on a condition
2156+
* based on regexp
21572157
2158-
```bash
2159-
$ # based on regex matching
2158+
```ruby
21602159
$ 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]/) * ":"'
21622161
foo:bar:baz
21632162
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
21642178
$ # to get index instead of matches
2179+
$ s='foo:123:bar:baz'
21652180
$ echo "$s" | ruby -F: -lane 'print $F.each_index.select{|i| $F[i] =~ /[a-z]/}'
21662181
[0, 2, 3]
21672182
@@ -2171,10 +2186,9 @@ $ echo "$s" | ruby -lane 'print $F.select { |s| s.to_i < 100 } * " "'
21712186
23 -983 5
21722187
21732188
$ # filters only those elements with successful substitution
2189+
$ # for opposite, either use negated condition or use reject instead of select
21742190
$ echo "$s" | ruby -lane 'print $F.select { |s| s.sub!(/3/, "E") } * " "'
21752191
2E -98E
2176-
2177-
$ # for opposite, either use negated condition or use reject instead of select
21782192
```
21792193
21802194
* random element(s)
@@ -2423,7 +2437,7 @@ $ # specify a negative count to preserve trailing empty fields
24232437
$ echo ':123::' | ruby -lne 'print $_.split(/:/, -1) * ","'
24242438
,123,,
24252439
2426-
$ # use string argument for fixed-string split instead of regex
2440+
$ # use string argument for fixed-string split instead of regexp
24272441
$ echo 'foo**123**baz' | ruby -lne 'print $_.split("**") * ":"'
24282442
foo:123:baz
24292443

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /