Skip to content

Commit 84485de

Browse files
authored
Merge pull request #5 from jdee/yaml-regexps
YAML regular expressions
2 parents a648163 + 70614b2 commit 84485de

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,18 @@ PatternPatch::Patch.new(
105105
```
106106

107107
If the `:binding` parameter is not passed, ERB is not invoked.
108+
109+
#### Regular expressions with modifiers in YAML
110+
111+
The `regexp` field in a YAML file may be specified with or without slashes
112+
or a modifier:
113+
114+
```YAML
115+
regexp: '^X' # Results in /^X/
116+
```
117+
118+
```YAML
119+
regexp: '/^X/i' # Results in /^X/i
120+
```
121+
122+
Currently only the slash literal notation is supported in YAML.

lib/pattern_patch/patch.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,23 @@ def from_yaml(path)
1717
# Adjust string fields from YAML
1818

1919
if hash[:regexp].kind_of? String
20-
hash[:regexp] = /#{hash[:regexp]}/
20+
regexp_string = hash[:regexp]
21+
if (matches = %r{^/(.+)/([imx]*)$}.match regexp_string)
22+
flags = 0
23+
if matches[2] =~ /i/
24+
flags |= Regexp::IGNORECASE
25+
end
26+
if matches[2] =~ /x/
27+
flags |= Regexp::EXTENDED
28+
end
29+
if matches[2] =~ /m/
30+
flags |= Regexp::MULTILINE
31+
end
32+
hash[:regexp] = Regexp.new matches[1], flags
33+
puts "Regexp from YAML: #{hash[:regexp].inspect}"
34+
else
35+
hash[:regexp] = /#{regexp_string}/
36+
end
2137
end
2238

2339
if hash[:mode].kind_of? String

lib/pattern_patch/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PatternPatch
2-
VERSION = "0.4.1"
2+
VERSION = "0.5.0"
33
end

0 commit comments

Comments
 (0)