Skip to content

Commit d4ae196

Browse files
corrections and some more misc examples
1 parent d32b407 commit d4ae196

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

chapters/Regular_expressions.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,10 @@ SyntaxError ((irb):4: invalid pattern in look-behind: /(?<!baz.*)123/)
15031503
=> {"1"=>"one", "2"=>"two", "4"=>"four"}
15041504
>> '9234012'.gsub(/[124]/) { h[$&] }
15051505
=> "9two3four0onetwo"
1506+
# or, simply pass hash variable as replacement argument
1507+
# if the matched text doesn't exist as a key, default value will be used
1508+
>> '9234012'.gsub(/[124]/, h)
1509+
=> "9two3four0onetwo"
15061510

15071511
# swap words
15081512
>> h = { 'cat' => 'tiger', 'tiger' => 'cat' }
@@ -1511,7 +1515,7 @@ SyntaxError ((irb):4: invalid pattern in look-behind: /(?<!baz.*)123/)
15111515
>> 'cat tiger dog tiger cat'.gsub(/\w+/) { h.key?($&) ? h[$&] : $& }
15121516
=> "tiger cat dog cat tiger"
15131517
# or build the regexp for simple cases
1514-
>> 'cat tiger dog tiger cat'.gsub(/cat|tiger/) { h[$&] }
1518+
>> 'cat tiger dog tiger cat'.gsub(/cat|tiger/, h)
15151519
=> "tiger cat dog cat tiger"
15161520
```
15171521

@@ -1521,27 +1525,45 @@ SyntaxError ((irb):4: invalid pattern in look-behind: /(?<!baz.*)123/)
15211525
>> h = { 'hand' => 1, 'handy' => 2, 'handful' => 3 }
15221526
=> {"hand"=>1, "handy"=>2, "handful"=>3}
15231527

1524-
>> 'handful hand pin handy'.gsub(Regexp.union(h.keys)) { h[$&] }
1528+
>> 'handful hand pin handy'.gsub(Regexp.union(h.keys), h)
15251529
=> "1ful 1 pin 1y"
15261530

15271531
>> r = Regexp.union(h.keys.sort_by { |w| -w.length })
15281532
=> /handful|handy|hand/
1529-
>> 'handful hand pin handy'.gsub(r) { h[$&] }
1533+
>> 'handful hand pin handy'.gsub(r, h)
15301534
=> "3 1 pin 2"
15311535
```
15321536

1537+
* left to right precedence of alternation can be exploited usefully in some cases
1538+
* for example, to extract quoted fields with `,` from simpler csv strings
1539+
* use proper csv parser if nature of input is not known
1540+
* See also [rexegg: best regex trick](https://www.rexegg.com/regex-best-trick.html)
1541+
1542+
```ruby
1543+
>> 'foo,"10,000",baz'.split(',')
1544+
=> ["foo", "\"10", "000\"", "baz"]
1545+
# specify regexp for quoted fields first
1546+
>> 'foo,"10,000",baz'.scan(/"[^"]+"|[^,]+/)
1547+
=> ["foo", "\"10,000\"", "baz"]
1548+
1549+
# such cases are a good place to use possessive quantifiers as well
1550+
>> "42 'good bye' 123".scan(/'[^']++'|[^ ]+/)
1551+
=> ["42", "'good bye'", "123"]
1552+
```
1553+
15331554
<br>
15341555

15351556
## <a name="further-reading"></a>Further Reading
15361557

15371558
Note that most of these resources are not specific to Ruby, so use them with caution and check if they apply to Ruby's syntax and features
15381559

15391560
* [rubular](http://rubular.com/) - Ruby regular expression editor
1561+
* [stackoverflow: ruby regexp](https://stackoverflow.com/questions/tagged/ruby+regex?sort=votes&pageSize=15)
15401562
* [stackoverflow: regex FAQ](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean)
15411563
* [rexegg](https://www.rexegg.com/) - comprehensive regular expression tutorials, tricks and more
15421564
* [regexcrossword](https://regexcrossword.com/) - tutorials and puzzles
1543-
* [swtch](https://swtch.com/~rsc/regexp/regexp1.html) - stuff about regular expression implementation engines
15441565
* [regexper](https://regexper.com/) - for visualization
1566+
* [swtch](https://swtch.com/~rsc/regexp/regexp1.html) - stuff about regular expression implementation engines
15451567

15461568

15471569

0 commit comments

Comments
 (0)