File tree 3 files changed +34
-0
lines changed
java/com/google/copybara/re2
javatests/com/google/copybara/re2
3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 195
195
- [ PathAttributes] ( #pathattributes )
196
196
- [ re2] ( #re2 )
197
197
- [ re2.compile] ( #re2compile )
198
+ - [ re2.quote] ( #re2quote )
198
199
- [ re2_matcher] ( #re2_matcher )
199
200
- [ re2_matcher.end] ( #re2_matcherend )
200
201
- [ re2_matcher.find] ( #re2_matcherfind )
@@ -4762,6 +4763,20 @@ re2.compile("a(.*)b").matches('accccb')
4762
4763
```
4763
4764
4764
4765
4766
+ <a id =" re2.quote " aria-hidden =" true " ></a >
4767
+ ### re2.quote
4768
+
4769
+ Quote a string to be matched literally if used within a regex pattern
4770
+
4771
+ ` string ` ` re2.quote(string) `
4772
+
4773
+
4774
+ #### Parameters:
4775
+
4776
+ Parameter | Description
4777
+ --------- | -----------
4778
+ string | ` string ` <br ><p ></p >
4779
+
4765
4780
4766
4781
4767
4782
## re2_matcher
Original file line number Diff line number Diff line change @@ -38,4 +38,13 @@ public class Re2Module implements StarlarkValue {
38
38
public StarlarkPattern compile (String regex ) {
39
39
return new StarlarkPattern (Pattern .compile (regex ));
40
40
}
41
+
42
+ @ StarlarkMethod (
43
+ name = "quote" ,
44
+ doc = "Quote a string to be matched literally if used within a regex pattern" ,
45
+ parameters = {
46
+ @ Param (name = "string" )})
47
+ public String quote (String string ) {
48
+ return Pattern .quote (string );
49
+ }
41
50
}
Original file line number Diff line number Diff line change @@ -184,4 +184,14 @@ public void replaceAllParam() throws ValidationException {
184
184
+ "m.matches()\n "
185
185
+ "x = m.replace_all('HELLO$1')" )).isEqualTo ("HELLObbHELLObbb" );
186
186
}
187
+
188
+ @ Test
189
+ public void quoteQuotes () throws ValidationException {
190
+ assertThat (skylark .<Boolean >eval ("x" , ""
191
+ + "re = re2.compile('a%s' % (re2.quote('.*')))\n "
192
+ + "x = re.matcher('axxxxxb').matches()\n " )).isFalse ();
193
+ assertThat (skylark .<Boolean >eval ("x" , ""
194
+ + "re = re2.compile('a%s' % (re2.quote('.*')))\n "
195
+ + "x = re.matcher('a.*').matches()\n " )).isTrue ();
196
+ }
187
197
}
You can’t perform that action at this time.
0 commit comments