[help] Regex pattern and raw strings

Hello folks! Another day, another noob question. Help wanted :3

I wrote this function to remove every ‘\n’ in the string.

let remove_newlines (str : string) : string =
    Str.global_replace (Str.regexp "\n") "" str

But then I discovered that it’s not the most elegant way to process a string like this

This is the first line.\nThis is the second line.

because there will be no space after the period mark and will make the text look less pretty.

So I tried

let remove_newlines (str : string) : string =
    Str.global_replace (Str.regexp {|\n(?![A-Z])|}) "" str

But I got no luck.

Also I’m not sure if I’m using {|STRING|} correctly. In Python, I use raw string in regexp like r"Some\nText\n" and I’m not familiar with OCaml’s raw strings.

What I want to achieve is:

>>> STRING = "Hello\nWorld, this i\ns your boi Anji"
>>> import re
>>> PATTERN=re.compile(r"\n(?![A-Z])")
>>> PATTERN.sub
PATTERN.sub(  PATTERN.subn(
>>> PATTERN.sub
<built-in method sub of re.Pattern object at 0x1095cb510>
>>> PATTERN.sub("", STRING)
'Hello\nWorld, this is your boi Anji'
>>>

Sorry for posting another n00b question. Send help plz :3

The problem was solved by writing a correct regexp

let remove_newlines (str : string) : string =
  let regex = Str.regexp "\n\\([a-z]\\)" in
  Str.global_replace regex " \\1" str

It turned out that it was my own problem for not having a good understanding of how to use regex. Sad!

you are not alone xkcd: Perl Problems

1 Like