forked from danlucraft/redcar
/
regex_replace.rb
221 lines (197 loc) · 5.46 KB
/
regex_replace.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
class RegexReplace
COND_RE = /\(
\?(\d+):
(
(
\\\(|
\\\)|
\\:|
[^():]
)*
)
(
:
(
(
\\\(|
\\\)|
[^()]
)*
)
)?
\)
/x
def initialize(re, replace)
if re.class.to_s.include? "Regexp"
@re = re
else
if defined? Oniguruma
@re = Oniguruma::ORegexp.new(re)
else
@re = Regexp.new(re)
end
end
@replace = replace
end
def regexp_gsub(re, string, &blk)
if re.class.to_s == "Oniguruma::ORegexp"
re.gsub(string, &blk)
else
left = string.dup
newstr = ""
while left.length > 0
md = re.match(left)
if md
newstr += md.pre_match
r = yield md
newstr += r
left = md.post_match
else
newstr += left
left = ""
end
end
newstr
end
end
def regexp_sub(re, string, &blk)
if re.class.to_s == "Oniguruma::ORegexp"
re.sub(string, &blk)
else
md = re.match(string)
if md
r = yield md
string[md.begin(0)..(md.end(0)-1)] = r
end
string
end
end
def grep(string)
string = string.dup
regexp_gsub(@re, string) do |md|
parse_replace_string md, @replace.dup
end
end
def rep(string)
string = string.dup
regexp_sub(@re, string) do |md|
parse_replace_string md, @replace.dup
end
end
def inspect
"<RegexReplace: #{@re.inspect} -> #{@replace.inspect}>"
end
private
def parse_replace_string(md, replace)
replace.gsub!(COND_RE) do |match|
if md[$1.to_i]
$2.gsub("\\(", "(").gsub("\\)", ")")
else
if $4
$5.gsub("\\(", "(").gsub("\\)", ")")
else
""
end
end
end
replace.gsub!(/\$(\d+)/) do |match|
md[$1.to_i]
end
replace.gsub!(/\\U(.*?)(\\E|$)/) do |match|
$1.upcase
end
replace.gsub!(/\\L(.*?)(\\E|$)/) do |match|
$1.downcase
end
replace.gsub!(/\\u(.)/) do |match|
$1.upcase
end
replace.gsub!(/\\l(.)/) do |match|
$1.downcase
end
replace.gsub!(/\\[abefnrstv]/) do |match|
eval("\"\\#{match[-1..-1]}\"")
end
replace.gsub!("\\u", "")
replace.gsub!("\\l", "")
replace.gsub!("\\U", "")
replace.gsub!("\\L", "")
replace.gsub!("\\E", "")
replace
end
end
if $0 == __FILE__
require 'test/unit'
class TestRegexReplace < Test::Unit::TestCase
def test_simple
rr = RegexReplace.new("blackbird",
"laura")
assert_equal "foo laura bar", rr.rep("foo blackbird bar")
end
def test_simple_blank
rr = RegexReplace.new("blackbird",
"laura")
assert_equal "", rr.rep("")
end
def test_simple_global
rr = RegexReplace.new("blackbird",
"laura")
assert_equal "foo laura bar laura bar", rr.grep("foo blackbird bar blackbird bar")
end
def test_simple_single
rr = RegexReplace.new("blackbird",
"laura")
assert_equal "foo laura bar blackbird bar", rr.rep("foo blackbird bar blackbird bar")
end
def test_escape_characters
rr = RegexReplace.new("blackbird",
"lau\\nr\\ta")
assert_equal "foo lau\nr\ta bar blackbird bar", rr.rep("foo blackbird bar blackbird bar")
end
def test_full_match
rr = RegexReplace.new("bl(ackb)ird",
"laura$0")
assert_equal "foo laurablackbird bar", rr.rep("foo blackbird bar")
end
def test_lonely_upcase
rr = RegexReplace.new("bl(ackb)ird",
"\\u")
assert_equal "foo bar", rr.rep("foo blackbird bar")
end
def test_capture1
rr = RegexReplace.new("bl(ackb)ird",
"laura$1")
assert_equal "foo lauraackb bar", rr.rep("foo blackbird bar")
end
def test_capture2
rr = RegexReplace.new("bl(ackb)i(r)d",
"$2laura$1")
assert_equal "foo rlauraackb bar", rr.rep("foo blackbird bar")
end
def test_upcase_string
rr = RegexReplace.new("bl(ackb)i(rd)",
"\\U$2\\Elaura$1")
assert_equal "foo RDlauraackb bar", rr.rep("foo blackbird bar")
end
def test_upcase_letter
rr = RegexReplace.new("bl(ackb)i(rd)",
"\\u$2laura$1")
assert_equal "foo Rdlauraackb bar", rr.rep("foo blackbird bar")
end
def test_combination
rr = RegexReplace.new("bl(ackb)i(rd)",
"\\l\\U$2\\Elaura$1")
assert_equal "foo rDlauraackb bar", rr.rep("foo blackbird bar")
end
def test_conditional
rr = RegexReplace.new("(\\w+)|(\\W+)",
"(?1:\\L$1\\E)(?2:\\(_)")
assert_equal "textmate(_power(_editing", rr.grep("TextMate: Power Editing")
end
def test_conditional_with_alternative
rr = RegexReplace.new("[[:alpha:]]+|( )",
"(?1:_:\\L$0)")
assert_equal "textmate:_power_editing", rr.grep("TextMate: Power Editing")
end
end
end