How to replace regular expression patterns in strings in Scala

Scala String FAQ: How do I replace a regular expression (regex) pattern in a String in Scala?


Because a String is immutable, you can’t perform find-and-replace operations directly on it, but you can create a new String that contains the replaced contents. There are several ways to do this.

You can call replaceAll on a String, remembering to assign the result to a new variable:

How to convert vi/vim tabs to spaces

vim tabs/spaces FAQ: How do I convert vi/vim tabs to spaces?

You can convert tabs to spaces in vim using the usual vim search and replace command. Specifically, this vim "tabs to spaces" command is all you need:

:1,$s/\t/  /g

In short, this command can be read like this:

Java - strip unwanted characters from a string

Here's a quick line of Java code that takes a given input string, strips all the characters from that string other than lowercase and uppercase letters, and returns whatever is left:

vi/vim search and replace: how to replace all tabs with spaces

vi/vim question: How do I convert all occurrences of ABC to XYZ in a file using the vi/vim editor?

Answer: This is actually easy to do with the vi or vim editor. Just go into "last line mode" by typing the ":" character, then enter a command like this to replace all occurrences of the string ABC with the string XYZ:

Java String replaceAll - why isn’t my “replace” method working?

Java String replace FAQ: Why isn't my Java String replace / replaceAll / replaceFirst code working?

The Java String class has several methods that usually make it really easy to replace one text pattern with another. I say "usually" because what occassionally happens is that I forget that a Java String is immutable (it can't be changed), so I accidentally try some String replace, replaceAll, or replaceFirst code that looks like this:

vim search and replace

vi/vim search and replace FAQ: How do I search and replace in vim?

A lot of people ask me how to perform a global search and replace operation in the vi (vim) editor. The vim editor is anything but intuitive, but for some reason I can remember this global search and replace syntax pretty easily.

A vim pattern search and delete example

vim FAQ: How do I perform a vim “search and delete” using a vim regular expression pattern (regex)?

Every once in a while when I’m using the vi editor (or vim) I find myself in a situation where I need to delete a bunch of lines in the file that match a particular pattern. In my younger days I used to get out of vi and then use sed or grep to get rid of those lines, but it turns out there’s a real easy way to do this in vi.

Perl array example - search replace in a Perl array

I was just modifying a Perl program so I could use a regular expression (regex) to search a Perl array for all less-than (<) and greater-than (>) symbols, and replace those with their HTML equivalents tags ("&lt;", and "&gt;", respectively).

Here's the source code I created to perform this Perl array search and replace operation (a Perl replace regex operation):