In Python, if you want to match literal parentheses, you need to escape them
Python 3.11.2 (main, Feb 16 2023, 03:20:12) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> TEXT = "Hello World! (Not really cuz I'm in a bad mood)"
>>> regex = re.compile("(\(.*\))")
>>> regex.findall(TEXT)
["(Not really cuz I'm in a bad mood)"]
In OCaml, if you escape parentheses, then you’re writing a group
; if you want to match literal parentheses, you do not need to escape them
utop # let text = "Hello World! (Not really)";;
val text : string = "Hello World! (Not really)"
utop # let regex = Str.regexp "\\(.*\\)";;
val regex : Str.regexp = <abstr>
utop # Str.global_replace regex "!!!!!!" text;;
- : string = "!!!!!!!!!!!!"
utop # let regex = Str.regexp "(.*)";;
val regex : Str.regexp = <abstr>
utop # Str.global_replace regex "!!!!!!" text;;
- : string = "Hello World! !!!!!!"
If you write in Python style
utop # let regex = Str.regexp "\(.*\)";;
Line 1, characters 24-26:
Warning 14 [illegal-backslash]: illegal backslash escape in string.
Line 1, characters 28-30:
Warning 14 [illegal-backslash]: illegal backslash escape in string.
val regex : Str.regexp = <abstr>
But it still works?
utop # Str.global_replace regex "!!!!!!" text;;
- : string = "!!!!!!!!!!!!"
I have to be honest, as a literal CS n00b who knows only some Python, it confuses me a lot…