Skip to content

Instantly share code, notes, and snippets.

@bfontaine
Last active February 19, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfontaine/6d55ff9009bce1a92dd3d44e5c4cdfd8 to your computer and use it in GitHub Desktop.
Save bfontaine/6d55ff9009bce1a92dd3d44e5c4cdfd8 to your computer and use it in GitHub Desktop.

Perl:

"test" =~ s/(.*)$/\1s/gr # => "testss

Ruby (same engine as Perl):

"test".gsub(/(.*)$/, "\\1s") # => "testss"

Java:

"test".replaceAll("(.*)$", "$1s") // => "testss"

Clojure (same engine as Java):

(clojure.string/replace "test" #"(.*)$" "$1s") ; => "testss"

JS:

"test".replace(/(.*)$/, "$1s") // => 'tests'

Python:

re.sub(r"(.*)$", "\\1s", "test") # => 'tests'

Go:

re := regexp.MustCompile("(.*)$")
re.ReplaceAllString("test", "${1}s") // => "tests"

Sed:

echo -n test | sed -E 's/(.*)$/\1s/g' # => "tests"

gawk:

echo test | gawk '{ print gensub(/(.*)$/, "\\1s", "g", $0) }' # => "tests"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment