Also is it possible to combine this with removing periods from within the string? The sentence may have spaces which I'd like to keep.
You can use special character sequences to put non-printable characters in your regular expression. Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).
You could remove all newlines with: s = s. replaceAll("\\n", ""); s = s. replaceAll("\\r", "");
Use the String. replace() method to remove all line breaks from a string, e.g. str. replace(/[\r\n]/gm, ''); . The replace() method will remove all line breaks from the string by replacing them with an empty string.
You need to strip \r and \n to remove carriage returns and new lines.
Search for
^[\r\n]+|\.|[\r\n]+$
and replace with nothing.
The specific syntax will depend on the language you're using, e. g. in C#:
resultString = Regex.Replace(subjectString, @"^[\r\n]+|\.|[\r\n]+$", "");
in PHP:
$result = preg_replace('/^[\r\n]+|\.|[\r\n]+$/', '', $subject);
in JavaScript:
result = subject.replace(/^[\r\n]+|\.|[\r\n]+$/g, "");
etc...
You can find:
^[\r\f]+|[\r\f]+$
and replace it with
''
For the periods you can find
\.
and replace it with
''
Some languages provide you with function what take a a group of regex and replacements and do the replacement in one call to the function. Like the PHP's preg_replace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With